Skip to content

Search in Rotated Sorted Array#8

Merged
Exzrgs merged 3 commits intomainfrom
arai60/search-in-rotated-sorted-array
May 28, 2024
Merged

Search in Rotated Sorted Array#8
Exzrgs merged 3 commits intomainfrom
arai60/search-in-rotated-sorted-array

Conversation

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The 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 right - left > 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.

while left > 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.

left < 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.

コメントありがとうございます!
自分的には、差を求めることによって区間幅を連想しやすくなるのでこうしてますね
右辺が0の場合はleft < rightでもすぐわかるのですが、left + 1 < rightだと自分は一度頭で変換しないと解釈できないです。

def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while right - left >= 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この条件は、while left <= right:の方が閉区間の条件だとパッと見て分かると思いました。

@Exzrgs Exzrgs merged commit bf74ddd into main May 28, 2024
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