---
# System prepended metadata

title: Leetcode 33. Search in Rotated Sorted Array

---

# Leetcode 33. Search in Rotated Sorted Array

## 題解

二分搜索

```python!
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        # Time complexity: O(logn)
        # Space complexity: O(1)
        left = 0
        right = len(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                return mid
            elif nums[left] <= nums[mid]: # 
                if target > nums[mid] or target < nums[left]:
                    left = mid + 1
                else:
                    right = mid - 1
            else:
                if target < nums[mid] or target > nums[right]:
                    right = mid - 1
                else:
                    left = mid + 1
        return -1
```