-
Notifications
You must be signed in to change notification settings - Fork 0
Search Insert Position #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,27 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package searchinsertposition | ||
|
|
||
| /* | ||
| 時間:12分 | ||
| 解法自体はすぐに浮かんで解くことができた。 | ||
| その割に二分探索を書くのがまだ慣れていなくて少し時間がかかってしまった。 | ||
| */ | ||
| func searchInsert_step1(nums []int, target int) int { | ||
| left, right := 0, len(nums) | ||
| var mid int | ||
| for left < right { | ||
| mid = (left + right) / 2 | ||
| if target == nums[mid] { | ||
| return mid | ||
| } | ||
| if target < nums[mid] { | ||
| right = mid | ||
| } else { | ||
| left = mid + 1 | ||
| } | ||
| } | ||
| if target < nums[mid] { | ||
| return mid | ||
| } | ||
| return mid + 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,22 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package searchinsertposition | ||
|
|
||
| /* | ||
| 最後の部分はleftを返せ馬良いと気づき変更した。 | ||
| その他もより読みやすいように直した。 | ||
| */ | ||
| func searchInsert_step2(nums []int, target int) int { | ||
| left, right := 0, len(nums) | ||
| for left < right { | ||
| mid := (left + right) / 2 | ||
| if nums[mid] == target { | ||
| return mid | ||
| } | ||
| if nums[mid] < target { | ||
| left = mid + 1 | ||
| } else { | ||
| right = mid | ||
| } | ||
rihib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return left | ||
| } | ||
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 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package searchinsertposition | ||
|
|
||
| /* | ||
| オーバーフローを起こさないようにmidの計算方法を修正した | ||
| https://github.com/hayashi-ay/leetcode/blob/064fca989bc4ecf9c7bce70237524a3e7ab1a21a/35.%20Search%20Insert%20Position.md?plain=1#L6 | ||
| */ | ||
| func searchInsert_step3(nums []int, target int) int { | ||
| left, right := 0, len(nums) | ||
| for left < right { | ||
| mid := left + (right-left)/2 | ||
rihib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if nums[mid] == target { | ||
| return mid | ||
rihib marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if nums[mid] < target { | ||
| left = mid + 1 | ||
| } else { | ||
| right = mid | ||
| } | ||
| } | ||
| return left | ||
| } | ||
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,15 @@ | ||
| //lint:file-ignore U1000 Ignore all unused code | ||
| package searchinsertposition | ||
|
|
||
| func searchInsert(nums []int, target int) int { | ||
| left, right := 0, len(nums) | ||
| for left < right { | ||
| mid := left + (right-left)/2 | ||
| if nums[mid] < target { | ||
| left = mid + 1 | ||
| } else { | ||
| right = mid | ||
| } | ||
| } | ||
| return left | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.