Skip to content

33. Search in Rotated Sorted Array#41

Open
5103246 wants to merge 1 commit intomainfrom
33-search-in-rotated-sorted-array
Open

33. Search in Rotated Sorted Array#41
5103246 wants to merge 1 commit intomainfrom
33-search-in-rotated-sorted-array

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Mar 8, 2026

}
}

if (nums[left] <= target && target <= nums.back()) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nums[left] <= target の条件は消しても動きますでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返信が遅くなってしまい、すいません。
targetが末尾以下なら、leftから末尾の範囲にtargetがあることがわかるので、nums[left] <= targetはなくても動きます。

}

if (nums[middle] <= nums[right]) {
if (nums[middle] < target && target <= nums[right]) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

念のため質問させてください。 target <= nums[right] の条件を消しても意図通り動きますでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target <= nums[right]を消したら、middleの位置を正しく分類できなくなるので、意図通りの動きにはなりません。

具体的には、[left , ... , target , ... , middle, ... , right], nums[middle] < targetのような場合、target <= nums[right]がないと、middleが「targetより左」の位置にあると分類してしまい、targetがない右側の領域を探索します。

nums[left] <= target && target < nums[middle] でも同様に、nums[left] <= targetを消すと意図した動きにはなりません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants