-
Notifications
You must be signed in to change notification settings - Fork 0
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
Single Number #48
Open
rihib
wants to merge
1
commit into
main
Choose a base branch
from
single_number
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
Single Number #48
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
This file contains 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,26 @@ | ||
//lint:file-ignore U1000 Ignore all unused code | ||
package singlenumber | ||
|
||
/* | ||
レビュワーの方へ: | ||
- このコードは既にGoの標準のフォーマッタで整形済みです。演算子の周りにスペースがあったりなかったりしますが、これはGoのフォーマッタによるもので、優先順位の高い演算子の周りにはスペースが入らず、低い演算子の周りには入るようになっています。https://qiita.com/tchssk/items/77030b4271cd192d0347 | ||
*/ | ||
|
||
/* | ||
時間:3分 | ||
LeetCodeには空間計算量がO(1)になるように実装しろと書かれていたが、方法が思いつかなかったので、まずは普通に実装してみた。 | ||
*/ | ||
func singleNumberStep1(nums []int) int { | ||
singleNums := make(map[int]int, len(nums)) | ||
for _, n := range nums { | ||
singleNums[n]++ | ||
if singleNums[n] > 1 { | ||
delete(singleNums, n) | ||
} | ||
} | ||
// 必要に応じてlen(singleNums) != 1のときはエラーを返すという処理も追加しても良いかも | ||
for n := range singleNums { | ||
return n | ||
} | ||
return 0 // 本来は見つからなかった場合はエラーを返したい | ||
} |
This file contains 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,19 @@ | ||
//lint:file-ignore U1000 Ignore all unused code | ||
package singlenumber | ||
|
||
/* | ||
レビュワーの方へ: | ||
- このコードは既にGoの標準のフォーマッタで整形済みです。演算子の周りにスペースがあったりなかったりしますが、これはGoのフォーマッタによるもので、優先順位の高い演算子の周りにはスペースが入らず、低い演算子の周りには入るようになっています。https://qiita.com/tchssk/items/77030b4271cd192d0347 | ||
*/ | ||
|
||
/* | ||
調べたところ下記のようにすれば空間計算量がO(1)になる。 | ||
ただし、このXORを使った方法は1つの数以外は全て偶数回必ず出現するという条件を満たさなければ成立しないので、実際にこのように書くのが良い方法だとは思わなかった(ただしビット演算の練習にはなる)。 | ||
*/ | ||
func singleNumberStep2(nums []int) int { | ||
singleNum := 0 | ||
for _, n := range nums { | ||
singleNum ^= n | ||
} | ||
return singleNum | ||
} |
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.
私は、この判定を次のループに回すと思います。そちらのほうがデバッグしやすそうです。
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.
これは趣味の範囲でしょう。
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.
デバッグのしやすさという観点でコードを見れてなかったので勉強になりました