-
Notifications
You must be signed in to change notification settings - Fork 0
35. Search Insert Position #21
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
Open
hiro111208
wants to merge
1
commit into
main
Choose a base branch
from
35-search-insert-position
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## 問題: [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/description/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Step 1 | ||
|
|
||
| - 二分探索で解く | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def searchInsert(self, nums: List[int], target: int) -> int: | ||
| start_index, end_index = 0, len(nums) - 1 | ||
| while start_index <= end_index: | ||
| middle_index = (start_index + end_index) // 2 | ||
| if nums[middle_index] == target: | ||
| return middle_index | ||
|
Comment on lines
+11
to
+12
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. この部分はあってももちろんいいんですが,ない方が多くの標準ライブラリにある二分探索の実装に近いですね. |
||
| elif nums[middle_index] < target: | ||
| start_index = middle_index + 1 | ||
| else: | ||
| end_index = middle_index - 1 | ||
| return start_index | ||
| ``` | ||
|
|
||
| 時間計算量: $O(\log n)$ | ||
|
|
||
| 空間計算量: $O(1)$ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Step 2 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def searchInsert(self, nums: List[int], target: int) -> int: | ||
| start, end = 0, len(nums) - 1 | ||
| while start <= end: | ||
| middle = start + (end - start) // 2 | ||
| if nums[middle] == target: | ||
| return middle | ||
| elif nums[middle] < target: | ||
| start = middle + 1 | ||
| else: | ||
| end = middle - 1 | ||
| return start | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def searchInsert(self, nums: List[int], target: int) -> int: | ||
| start, end = 0, len(nums) - 1 | ||
| while start <= end: | ||
| middle = start + (end - start) // 2 | ||
| if nums[middle] == target: | ||
| return middle | ||
| elif nums[middle] > target: | ||
| end = middle - 1 | ||
| else: | ||
| start = middle + 1 | ||
| return start | ||
| ``` | ||
|
|
||
| 1回目: 1分 31秒 | ||
|
|
||
| 2回目: 1分 26秒 | ||
|
|
||
| 3回目: 1分 27秒 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
原則 1 行に 1 つの代入文を書いたほうが、読み手にとって読みやすくなると思います。 swap をする場合は例外だと思います。