File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
search-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ μκ° λ³΅μ‘λ: O(log n)
3
+ - μ΄μ§ νμ(Binary Search)μ μ¬μ©νμ¬ λ°°μ΄μ μ λ°μ© λλλ©° νμνλ―λ‘ O(log n)μ
λλ€.
4
+
5
+ κ³΅κ° λ³΅μ‘λ: O(1)
6
+ - μΆκ°μ μΈ λ°°μ΄μ μ¬μ©νμ§ μκ³ , λͺ κ°μ λ³μλ§ μ¬μ©νλ―λ‘ O(1)μ
λλ€.
7
+ '''
8
+
9
+ from typing import List
10
+
11
+ class Solution :
12
+ def search (self , nums : List [int ], target : int ) -> int :
13
+ left , right = 0 , len (nums ) - 1
14
+
15
+ while left <= right :
16
+ mid = (left + right ) // 2
17
+
18
+ if nums [mid ] == target :
19
+ return mid
20
+
21
+ # μΌμͺ½ μ λ°μ΄ μ λ ¬λμ΄ μλ κ²½μ°
22
+ if nums [left ] <= nums [mid ]:
23
+ if nums [left ] <= target < nums [mid ]: # νκ²μ΄ μΌμͺ½ λ²μ λ΄μ μμ
24
+ right = mid - 1
25
+ else : # νκ²μ΄ μ€λ₯Έμͺ½ λ²μμ μμ
26
+ left = mid + 1
27
+ # μ€λ₯Έμͺ½ μ λ°μ΄ μ λ ¬λ κ²½μ°
28
+ else :
29
+ if nums [mid ] < target <= nums [right ]: # νκ²μ΄ μ€λ₯Έμͺ½ λ²μ λ΄μ μμ
30
+ left = mid + 1
31
+ else : # νκ²μ΄ μΌμͺ½ λ²μμ μμ
32
+ right = mid - 1
33
+
34
+ return - 1
You canβt perform that action at this time.
0 commit comments