Skip to content

1. Two Sum#11

Open
n6o wants to merge 1 commit intomainfrom
two-sum
Open

1. Two Sum#11
n6o wants to merge 1 commit intomainfrom
two-sum

Conversation

@n6o
Copy link
Copy Markdown
Owner

@n6o n6o commented Feb 8, 2026

今回の問題

Two Sum - LeetCode

使用言語

Go

次に解く問題

Group Anagrams - LeetCode

func twoSum(nums []int, target int) []int {
numToIndex := make(map[int]int, len(nums))

for i, n := range nums {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

些末ですが…
numToIndex を定義しているので、n は合わせて num が一貫性の面で良いと思いました。

Suggested change
for i, n := range nums {
for i, num := range nums {

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.

ありがとうございます。
num のほうが numToIndex との関連が連想できて読みやすそうだと思いました。

numToIndex[n] = i
}

return nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Go的には、見つかったかどうかのboolと空のスライスを返すのが自然に感じました(前者はLeetCodeのシグネチャ上、無理なのですが…)。
あるいは panic とかですかね。

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.

そうですね、ここは少し悩みました。
実務であれば記載いただいたようにすると思います。
環境の制約でできないので、leetcode の環境であるという前提で、 panic で何が起こったのかを明示するようにした方がわかりやすそうですね。

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.

2 participants