Skip to content

Commit 1153d2b

Browse files
authored
feat: add solution to lc problem: No.0033 (#4062)
1 parent b389888 commit 1153d2b

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0000-0099/0033.Search in Rotated Sorted Array/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,34 @@ var search = function (nums, target) {
281281
};
282282
```
283283

284+
#### C#
285+
286+
```cs
287+
public class Solution {
288+
public int Search(int[] nums, int target) {
289+
int n = nums.Length;
290+
int left = 0, right = n - 1;
291+
while (left < right) {
292+
int mid = (left + right) >> 1;
293+
if (nums[0] <= nums[mid]) {
294+
if (nums[0] <= target && target <= nums[mid]) {
295+
right = mid;
296+
} else {
297+
left = mid + 1;
298+
}
299+
} else {
300+
if (nums[mid] < target && target <= nums[n - 1]) {
301+
left = mid + 1;
302+
} else {
303+
right = mid;
304+
}
305+
}
306+
}
307+
return nums[left] == target ? left : -1;
308+
}
309+
}
310+
```
311+
284312
#### PHP
285313

286314
```php

solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@ var search = function (nums, target) {
269269
};
270270
```
271271

272+
#### C#
273+
274+
```cs
275+
public class Solution {
276+
public int Search(int[] nums, int target) {
277+
int n = nums.Length;
278+
int left = 0, right = n - 1;
279+
while (left < right) {
280+
int mid = (left + right) >> 1;
281+
if (nums[0] <= nums[mid]) {
282+
if (nums[0] <= target && target <= nums[mid]) {
283+
right = mid;
284+
} else {
285+
left = mid + 1;
286+
}
287+
} else {
288+
if (nums[mid] < target && target <= nums[n - 1]) {
289+
left = mid + 1;
290+
} else {
291+
right = mid;
292+
}
293+
}
294+
}
295+
return nums[left] == target ? left : -1;
296+
}
297+
}
298+
```
299+
272300
#### PHP
273301

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

0 commit comments

Comments
 (0)