Skip to content

Commit ac00bfb

Browse files
authored
Update readme.md
1 parent f93253a commit ac00bfb

File tree

1 file changed

+69
-1
lines changed
  • src/main/java/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array

1 file changed

+69
-1
lines changed

src/main/java/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,72 @@ You must write an algorithm with `O(log n)` runtime complexity.
3131
* <code>0 <= nums.length <= 10<sup>5</sup></code>
3232
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
3333
* `nums` is a non-decreasing array.
34-
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
35+
36+
To solve the "Find First and Last Position of Element in Sorted Array" problem in Java with a `Solution` class, we can follow these steps:
37+
38+
1. Define a `Solution` class.
39+
2. Define a method named `searchRange` that takes an integer array `nums` and an integer `target` as input and returns an integer array representing the starting and ending positions of `target` in `nums`. If `target` is not found, return `[-1, -1]`.
40+
3. Implement binary search to find the first and last occurrences of `target`.
41+
4. Set the left pointer `left` to 0 and the right pointer `right` to the length of `nums` minus 1.
42+
5. Initialize two variables `firstOccurrence` and `lastOccurrence` to -1.
43+
6. Perform two binary search operations:
44+
- First, find the first occurrence of `target`:
45+
- While `left` is less than or equal to `right`:
46+
- Calculate the middle index `mid` as `(left + right) / 2`.
47+
- If `nums[mid]` is equal to `target`, update `firstOccurrence = mid` and continue searching on the left half by updating `right = mid - 1`.
48+
- Otherwise, if `target` is less than `nums[mid]`, update `right = mid - 1`.
49+
- Otherwise, update `left = mid + 1`.
50+
- Second, find the last occurrence of `target`:
51+
- Reset `left` to 0 and `right` to the length of `nums` minus 1.
52+
- While `left` is less than or equal to `right`:
53+
- Calculate the middle index `mid` as `(left + right) / 2`.
54+
- If `nums[mid]` is equal to `target`, update `lastOccurrence = mid` and continue searching on the right half by updating `left = mid + 1`.
55+
- Otherwise, if `target` is greater than `nums[mid]`, update `left = mid + 1`.
56+
- Otherwise, update `right = mid - 1`.
57+
7. Return the array `[firstOccurrence, lastOccurrence]`.
58+
59+
Here's the implementation:
60+
61+
```java
62+
public class Solution {
63+
public int[] searchRange(int[] nums, int target) {
64+
int left = 0;
65+
int right = nums.length - 1;
66+
int firstOccurrence = -1;
67+
int lastOccurrence = -1;
68+
69+
// Find first occurrence
70+
while (left <= right) {
71+
int mid = left + (right - left) / 2;
72+
if (nums[mid] == target) {
73+
firstOccurrence = mid;
74+
right = mid - 1;
75+
} else if (target < nums[mid]) {
76+
right = mid - 1;
77+
} else {
78+
left = mid + 1;
79+
}
80+
}
81+
82+
// Find last occurrence
83+
left = 0;
84+
right = nums.length - 1;
85+
while (left <= right) {
86+
int mid = left + (right - left) / 2;
87+
if (nums[mid] == target) {
88+
lastOccurrence = mid;
89+
left = mid + 1;
90+
} else if (target < nums[mid]) {
91+
right = mid - 1;
92+
} else {
93+
left = mid + 1;
94+
}
95+
}
96+
97+
return new int[]{firstOccurrence, lastOccurrence};
98+
}
99+
}
100+
```
101+
102+
This implementation provides a solution to the "Find First and Last Position of Element in Sorted Array" problem in Java. It returns the starting and ending positions of `target` in `nums` using binary search, with a time complexity of O(log n).

0 commit comments

Comments
 (0)