Skip to content

Commit 6ac504d

Browse files
committed
34. Find First and Last Position of Element in Sorted Array
1 parent 913a753 commit 6ac504d

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/leetcode/_34_/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._34_;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
System.out.println(Arrays.toString(solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6)));
12+
}
13+
}
14+

src/leetcode/_34_/Solution.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package leetcode._34_;
2+
3+
class Solution {
4+
public int[] searchRange(int[] nums, int target) {
5+
int[] result = new int[]{-1, -1};
6+
if (nums.length == 0) {
7+
return result;
8+
}
9+
int i = 0;
10+
int j = nums.length - 1;
11+
while (i < j) {
12+
int mid = (i + j) / 2;
13+
if (nums[mid] < target) {
14+
i = mid + 1;
15+
} else {
16+
j = mid;
17+
}
18+
}
19+
if (nums[i] != target)
20+
return result;
21+
result[0] = i;
22+
j = nums.length - 1;
23+
while (i < j) {
24+
int mid = (i + j) / 2 + 1; // 中位数偏右,避免上面的判断无法跳出循环
25+
if (nums[mid] > target) {
26+
j = mid - 1;
27+
} else {
28+
i = mid;
29+
}
30+
}
31+
if (nums[j] == target) {
32+
result[1] = j;
33+
}
34+
return result;
35+
}
36+
}

src/leetcode/_34_/solution.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
### [34\. Find First and Last Position of Element in Sorted ArrayCopy for Markdown](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array of integers `nums` sorted in ascending order, find the starting and ending position of a given `target` value.
7+
8+
Your algorithm's runtime complexity must be in the order of _O_(log _n_).
9+
10+
If the target is not found in the array, return `[-1, -1]`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [5,7,7,8,8,10], target = 8
16+
Output: [3,4]```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: nums = [5,7,7,8,8,10], target = 6
22+
Output: [-1,-1]```
23+
24+
25+
#### Solution
26+
27+
Language: **Java**
28+
29+
```java
30+
class Solution {
31+
   public int[] searchRange(int[] nums, int target) {
32+
       int[] result = new int[]{-1, -1};
33+
       if (nums.length == 0) {
34+
           return result;
35+
      }
36+
       int i = 0;
37+
       int j = nums.length - 1;
38+
       while (i < j) {
39+
           int mid = (i + j) / 2;
40+
           if (nums[mid] < target) {
41+
               i = mid + 1;
42+
          } else {
43+
               j = mid;
44+
          }
45+
      }
46+
       if (nums[i] != target)
47+
           return result;
48+
       result[0] = i;
49+
       j = nums.length - 1;
50+
       while (i < j) {
51+
           int mid = (i + j) / 2 + 1; // 中位数偏右,避免上面的判断无法跳出循环
52+
           if (nums[mid] > target) {
53+
               j = mid - 1;
54+
          } else {
55+
               i = mid;
56+
          }
57+
      }
58+
       if (nums[j] == target) {
59+
           result[1] = j;
60+
      }
61+
       return result;
62+
  }
63+
}
64+
```
65+
![](https://ws4.sinaimg.cn/large/006tKfTcgy1g13hk8nrndj310s0r042m.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy