You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
publicclassSolution {
63
+
publicint[] searchRange(int[] nums, inttarget) {
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
+
} elseif (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
+
} elseif (target < nums[mid]) {
91
+
right = mid -1;
92
+
} else {
93
+
left = mid +1;
94
+
}
95
+
}
96
+
97
+
returnnewint[]{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