Skip to content

Commit 960c5f6

Browse files
authoredFeb 19, 2025··
feat: add solution to lc problem: No.0034 (#4077)
1 parent 3bb964b commit 960c5f6

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
 

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,31 @@ var searchRange = function (nums, target) {
211211
};
212212
```
213213

214+
#### C#
215+
216+
```cs
217+
public class Solution {
218+
public int[] SearchRange(int[] nums, int target) {
219+
int l = Search(nums, target);
220+
int r = Search(nums, target + 1);
221+
return l == r ? new int[] {-1, -1} : new int[] {l, r - 1};
222+
}
223+
224+
private int Search(int[] nums, int x) {
225+
int left = 0, right = nums.Length;
226+
while (left < right) {
227+
int mid = (left + right) >>> 1;
228+
if (nums[mid] >= x) {
229+
right = mid;
230+
} else {
231+
left = mid + 1;
232+
}
233+
}
234+
return left;
235+
}
236+
}
237+
```
238+
214239
#### PHP
215240

216241
```php

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ var searchRange = function (nums, target) {
201201
};
202202
```
203203

204+
#### C#
205+
206+
```cs
207+
public class Solution {
208+
public int[] SearchRange(int[] nums, int target) {
209+
int l = Search(nums, target);
210+
int r = Search(nums, target + 1);
211+
return l == r ? new int[] {-1, -1} : new int[] {l, r - 1};
212+
}
213+
214+
private int Search(int[] nums, int x) {
215+
int left = 0, right = nums.Length;
216+
while (left < right) {
217+
int mid = (left + right) >>> 1;
218+
if (nums[mid] >= x) {
219+
right = mid;
220+
} else {
221+
left = mid + 1;
222+
}
223+
}
224+
return left;
225+
}
226+
}
227+
```
228+
204229
#### PHP
205230

206231
```php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int[] SearchRange(int[] nums, int target) {
3+
int l = Search(nums, target);
4+
int r = Search(nums, target + 1);
5+
return l == r ? new int[] {-1, -1} : new int[] {l, r - 1};
6+
}
7+
8+
private int Search(int[] nums, int x) {
9+
int left = 0, right = nums.Length;
10+
while (left < right) {
11+
int mid = (left + right) >>> 1;
12+
if (nums[mid] >= x) {
13+
right = mid;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
return left;
19+
}
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.