Skip to content

Create 33. Search in Rotated Sorted Array.md#43

Open
fuga-98 wants to merge 1 commit into
mainfrom
33.-Search-in-Rotated-Sorted-Array
Open

Create 33. Search in Rotated Sorted Array.md#43
fuga-98 wants to merge 1 commit into
mainfrom
33.-Search-in-Rotated-Sorted-Array

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 12, 2025

break_point = search_break()
if target <= nums[-1]:
result = bisect_left(nums, target, lo=break_point, hi=len(nums))
if result >= len(nums) or nums[result] != target:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

result == len(nums) の場合、target > nums[-1] なので外側の if で弾かれており、不要な条件になると思います。

if result >= len(nums) or nums[result] != target:
return -1

これの最初の条件いらなそう
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

compare関数の戻り値の最初の要素はnumsの末尾が必ず1になることを考慮すると、result == len(nums) になる場合は、compare(nums[i]) の結果が全て (0,0), (1,0) のいずれかになって、compare(target) の結果が (1,1) になる場合。でも、target <= nums[-1] の場合、nums[i] >= target となる i が必ず存在するので、result == len(nums) にはなり得ない、ということですね。
いらない理由を考えるのに結構頭を使ったのでコメントに書いた方が良さそうと思いました。

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.

レビューありがとうございます。
正確に書いていただいて非常に助かります。

3回書く中でコメントを書くと大変なので省略してしまっていますが、実務なら書いたほうが良いですね。


[TF,TF, TF] t= FTとなるのでresult=0となります

key関数の命名がうまいものが思いつかないが、実務ならdocstringで説明を書くで十分な気がしている。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

現実問題、これは関数の名前だけで動作原理を説明し切ることは難しいでしょうね。

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.

3 participants