-
Notifications
You must be signed in to change notification settings - Fork 0
033-search-in-rotated-array #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ## step1で考えたこと | ||
|
|
||
| 前回の問題と似たように、L,R,Mを使って、幅を狭めていく、Targetと一致すればその地点のIndexをreturn | ||
| なければ return -1 | ||
| ただ、今回のArrayもrotateされているから、シンプルに、nums[mid]とtargetを比較しても | ||
| 分割したパーテーションに対象の値がなく、もう一方の方に存在する時にどうIndexを動かしていけば良いかがわからない。 | ||
|
|
||
| ## step2 | ||
| Step1としては、 | ||
| 「左半分 or 右半分のどっちかは必ず昇順」 | ||
| 「昇順側に target が入っているか範囲で見る」 | ||
| という発想に到達できるとベストだった。 | ||
|
|
||
| なぜ、普通の二分探索の方法が使えないのかを分析した上で、 | ||
| 左、右のどちらかはソートされていることに気づけていれば正解に近づけた気がする。 | ||
|
|
||
| elif nums[mid] > nums[left]: | ||
| -> "="のつけ忘れ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[mid] > target: | ||
|
|
||
| else: | ||
|
|
||
|
|
||
| return -1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ## 解答解説を見た後に自力で書いたコード | ||
|
|
||
| class Solution: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 仮に自分がこの問題を解くとしたら、 |
||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left <= right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[mid] > nums[left]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 他の個所が < または <= で統一されているにもかかわらず、ここだけ > なのが気になりました。 < に統一するとよいと思います。 |
||
| if nums[left] <= target < nums[mid]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 念のため確認させてください。 |
||
| right = mid - 1 | ||
| else: | ||
| left = mid + 1 | ||
| else: | ||
| if nums[mid] < target <= nums[right]: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid - 1 | ||
|
|
||
| return -1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left <= right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] == target: | ||
| return mid | ||
| elif nums[left] <= nums[mid]: | ||
| if nums[left] <= target < nums[mid]: | ||
| right = mid - 1 | ||
| else: | ||
| left = mid + 1 | ||
| else: | ||
| if nums[mid] < target <= nums[right]: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid - 1 | ||
| return -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
左半分も右半分もどちらも必ず照準に並んでいると思います。