|
| 1 | +--- |
| 2 | +title: 33. Search in Rotated Sorted Array |
| 3 | +subtitle: "https://leetcode.com/problems/search-in-rotated-sorted-array/description/" |
| 4 | +date: 2024-03-31T16:26:00+08:00 |
| 5 | +lastmod: 2024-03-31T16:26:00+08:00 |
| 6 | +draft: false |
| 7 | +author: "Kimi.Tsai" |
| 8 | +authorLink: "https://kimi0230.github.io/" |
| 9 | +description: "0033.Search-in-Rotated-Sorted-Array" |
| 10 | +license: "" |
| 11 | +images: [] |
| 12 | + |
| 13 | +tags: [LeetCode, Go, Medium, Array, Binary Search, 33. Search in Rotated Sorted Array, Amazon,Facebook, Microsoft, LinkedIn, Apple] |
| 14 | +categories: [LeetCode] |
| 15 | + |
| 16 | +featuredImage: "" |
| 17 | +featuredImagePreview: "" |
| 18 | + |
| 19 | +hiddenFromHomePage: false |
| 20 | +hiddenFromSearch: false |
| 21 | +twemoji: false |
| 22 | +lightgallery: true |
| 23 | +ruby: true |
| 24 | +fraction: true |
| 25 | +fontawesome: true |
| 26 | +linkToMarkdown: false |
| 27 | +rssFullText: false |
| 28 | + |
| 29 | +toc: |
| 30 | + enable: true |
| 31 | + auto: true |
| 32 | +code: |
| 33 | + copy: true |
| 34 | + maxShownLines: 200 |
| 35 | +math: |
| 36 | + enable: false |
| 37 | + # ... |
| 38 | +mapbox: |
| 39 | + # ... |
| 40 | +share: |
| 41 | + enable: true |
| 42 | + # ... |
| 43 | +comment: |
| 44 | + enable: true |
| 45 | + # ... |
| 46 | +library: |
| 47 | + css: |
| 48 | + # someCSS = "some.css" |
| 49 | + # located in "assets/" |
| 50 | + # Or |
| 51 | + # someCSS = "https://cdn.example.com/some.css" |
| 52 | + js: |
| 53 | + # someJS = "some.js" |
| 54 | + # located in "assets/" |
| 55 | + # Or |
| 56 | + # someJS = "https://cdn.example.com/some.js" |
| 57 | +seo: |
| 58 | + images: [] |
| 59 | + # ... |
| 60 | +--- |
| 61 | +# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) |
| 62 | + |
| 63 | +## 題目 |
| 64 | +There is an integer array nums sorted in ascending order (with distinct values). |
| 65 | + |
| 66 | +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. |
| 67 | + |
| 68 | +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. |
| 69 | + |
| 70 | +You must write an algorithm with O(log n) runtime complexity. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +Example 1: |
| 75 | + |
| 76 | +Input: nums = [4,5,6,7,0,1,2], target = 0 |
| 77 | +Output: 4 |
| 78 | +Example 2: |
| 79 | + |
| 80 | +Input: nums = [4,5,6,7,0,1,2], target = 3 |
| 81 | +Output: -1 |
| 82 | +Example 3: |
| 83 | + |
| 84 | +Input: nums = [1], target = 0 |
| 85 | +Output: -1 |
| 86 | + |
| 87 | + |
| 88 | +Constraints: |
| 89 | + |
| 90 | +1 <= nums.length <= 5000 |
| 91 | +-104 <= nums[i] <= 104 |
| 92 | +All values of nums are unique. |
| 93 | +nums is an ascending array that is possibly rotated. |
| 94 | +-104 <= target <= 104 |
| 95 | + |
| 96 | +## 題目大意 |
| 97 | +整個數組按照升序排序, 值不相同 |
| 98 | +在傳遞給函數之前, nums 在預先未知的某個下標 k ( 0 <= k < nums.length )上進行了 旋轉,使數位變數 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (下標 從 0 開始 計數)。 例如, [0,1,2,4,5,6,7] 在下標 3 處經旋轉後可能變為 [4,5,6,7,0,1,2] 。 |
| 99 | + |
| 100 | +給你 旋轉後 的陣列 nums 和一個整數 target ,如果 nums 中存在這個目標值 target ,則返回它的下標,否則返回 -1 。 |
| 101 | + |
| 102 | +你必須設計一個時間複雜度為 O(log n) 的演演算法解決此問題。 |
| 103 | + |
| 104 | +## 解題思路 |
| 105 | + |
| 106 | +先判斷mid位於上半部還是下半部, 再來找target位於哪邊 |
| 107 | +最後看target是否有在左或右端點 |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +## Big O |
| 113 | + |
| 114 | +* 時間複雜 : `O(log n)` |
| 115 | +* 空間複雜 : `O(1)` |
| 116 | + |
| 117 | +## 來源 |
| 118 | +* https://leetcode.com/problems/search-in-rotated-sorted-array/description/ |
| 119 | +* https://leetcode.cn/problems/ |
| 120 | + |
| 121 | +## 解答 |
| 122 | +https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0033.Search-in-Rotated-Sorted-Array/main.go |
| 123 | + |
| 124 | +```go |
| 125 | +package searchinrotatedsortedarray |
| 126 | + |
| 127 | +// 時間複雜 O(), 空間複雜 O() |
| 128 | +func search(nums []int, target int) int { |
| 129 | + if nums == nil || len(nums) == 0 { |
| 130 | + return -1 |
| 131 | + } |
| 132 | + left, right := 0, len(nums)-1 |
| 133 | + for left < right { |
| 134 | + mid := int(uint(left+right) >> 1) |
| 135 | + |
| 136 | + if nums[mid] == target { |
| 137 | + return mid |
| 138 | + } |
| 139 | + |
| 140 | + if nums[mid] >= nums[left] { |
| 141 | + // mid在上半區 |
| 142 | + |
| 143 | + if nums[left] <= target && target <= nums[mid] { |
| 144 | + // target 在left跟min中間 |
| 145 | + right = mid - 1 |
| 146 | + } else { |
| 147 | + left = mid + 1 |
| 148 | + } |
| 149 | + } else { |
| 150 | + // mid在下半區 |
| 151 | + if nums[mid] <= target && target <= nums[right] { |
| 152 | + // target 在min跟right中間 |
| 153 | + left = mid + 1 |
| 154 | + } else { |
| 155 | + right = mid - 1 |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + if nums[left] == target { |
| 160 | + return left |
| 161 | + } else if nums[right] == target { |
| 162 | + return right |
| 163 | + } else { |
| 164 | + return -1 |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +``` |
| 169 | + |
| 170 | +## Benchmark |
| 171 | + |
| 172 | +```sh |
| 173 | + |
| 174 | +``` |
0 commit comments