File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
search-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // 33. Search in Rotated Sorted Array
3
+ // https://leetcode.com/problems/search-in-rotated-sorted-array/description/
4
+ // Dale-Study
5
+ //
6
+ // Created by WhiteHyun on 2024/06/10.
7
+ //
8
+
9
+ final class Solution {
10
+ func search( _ nums: [ Int ] , _ target: Int ) -> Int {
11
+ guard nums. count != 1
12
+ else {
13
+ return nums [ 0 ] == target ? 0 : - 1
14
+ }
15
+
16
+ var left = 0
17
+ var right = nums. count - 1
18
+
19
+ while left <= right {
20
+ let mid = ( left + right) >> 1
21
+
22
+ if nums [ mid] == target { return mid }
23
+
24
+ if nums [ left] <= nums [ mid] {
25
+ if target > nums [ mid] || target < nums [ left] {
26
+ left = mid + 1
27
+ } else {
28
+ right = mid - 1
29
+ }
30
+ } else {
31
+ if target < nums [ mid] || target > nums [ right] {
32
+ right = mid - 1
33
+ } else {
34
+ left = mid + 1
35
+ }
36
+ }
37
+ }
38
+
39
+ return - 1
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments