Skip to content

Conversation

@Fuminiton
Copy link
Owner

@Fuminiton Fuminiton changed the title 33. Search in Rotated Sorted Array Solved: 33. Search in Rotated Sorted Array Jun 1, 2025
4. numが後半に属し、target以上 -> (True, True)
<- targetが後半ならこれを満たす初めを得る
"""
return (num <= nums[-1], target <= num)
Copy link

Choose a reason for hiding this comment

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

あんまり考えていないのですが、これ、単に
(num <= nums[-1], num)
でもいいですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。

これ指摘されて今気づけました。

前半と後半のどちらにいるかを分けた後に、target以上になる位置を探したいので、
(num <= nums[-1], num)で問題ないですね。

return left
return -1

if nums is None:
Copy link

Choose a reason for hiding this comment

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

  • numsが空のケースを弾く

空が空配列のことなら弾けてないですね。Noneと[]は別物なので。

$ ipython
Python 3.11.3 (main, Jun  5 2023, 00:49:13) [Clang 14.0.3 (clang-1403.0.22.14.1)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: [] is None
Out[1]: False

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘、検証ありがとうございます。
空配列とNoneを混同していました。

以下のようにすべきですね。

    if nums is None:
        raise TypeError("Input cannot be None")
    if len(nums) == 0:
        raise ValueError("Input cannot be empty array")

Copy link

Choose a reason for hiding this comment

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

numsの型がList[int]と書いてある (List[int] | None ではない) のでNoneかどうかだけわざわざチェックするのはちょっと違和感ありました。じゃあnumsがfloatとかdictのときとかをスルーしてるの何でだろうと思うので。
自分があえてやるなら not isinstance(nums, list) とか type(nums) is not list でTypeError出すかなぁと思いました (サブタイプも受け入れたいから前者かな)。

Copy link
Owner Author

Choose a reason for hiding this comment

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

じゃあnumsがfloatとかdictのときとかをスルーしてるの何でだろうと思うので。

おっしゃる通りです。
今回は、if len(nums) == 0:だけしておけばよさそうですね。

自分があえてやるなら not isinstance(nums, list) とか type(nums) is not list でTypeError出すかなぁと思いました (サブタイプも受け入れたいから前者かな)。

確かに、型が一致するか(サブタイプ含めて一致するかみるならtype、そうでないならisinstanceを選ぶ)を見る方が、適切ですね。

return target <= num
else: # numが後半の山にいる
return True
if target <= nums[-1]: # targetが後半の山にいると思われる
Copy link

Choose a reason for hiding this comment

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

else: にしてもよいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
確かにこれくらいの距離ならelseでも条件を覚えておけそうですね。

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.

5 participants