Skip to content

Conversation

@syoshida20
Copy link
Owner

@syoshida20 syoshida20 commented May 5, 2025

問題URL

https://leetcode.com/problems/two-sum/

問題文

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

Example 1:

  • Input: nums = [2,7,11,15], target = 9
  • Output: [0,1]
  • Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

  • Input: nums = [3,2,4], target = 6
  • Output: [1,2]

Example 3:

  • Input: nums = [3,3], target = 6
  • Output: [0,1]

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.

Follow-up

Can you come up with an algorithm that is less than O(n2) time complexity?

@syoshida20 syoshida20 force-pushed the feature/hash-map/two-sum branch from 2b1aca6 to edaa491 Compare May 5, 2025 08:09
@syoshida20 syoshida20 self-assigned this May 5, 2025
@syoshida20 syoshida20 marked this pull request as ready for review May 5, 2025 14:08
}
num_to_index_table.set(num, i)
}
return new Error(`The pair is not found for the target value (${value}).`)
Copy link

Choose a reason for hiding this comment

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

JavaScript は throw じゃなかったでしたっけ。

Copy link
Owner Author

@syoshida20 syoshida20 May 5, 2025

Choose a reason for hiding this comment

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

実行が中断されることを期待していたのですが、
throwでExceptionを投げるべきでした。

throwが正しく理解できていなかったため、ドキュメントを読みました。

中断が行われ、制御は最も近いcatch文に移行される。

参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

Execution of the current function will stop (the statements after throw won't be executed), control will be passed to the first catch block in the call stack.

Copy link
Owner Author

Choose a reason for hiding this comment

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

402afc1 でレビューを反映したコードを追加しました。

};
```

* mapをもっと綺麗にかける。
Copy link

Choose a reason for hiding this comment

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

「綺麗に」と言われるとやや違和感がありますね。
返したい値が多かったり色々なところから持ってくるような時とかは前者の方が見やすいので、限定的だとは思います

Copy link
Owner Author

@syoshida20 syoshida20 May 10, 2025

Choose a reason for hiding this comment

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

ケースバイケースで、一長一短ということですね。

返したい値が多かったり色々なところから持ってくるような時とかは前者の方が見やすい

上のコメントをいただき、自分の認識が誤っていたことに気づきました。

Copy link

Choose a reason for hiding this comment

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

誤ってるかは微妙ですが、、、適当な長さでいくつか書いてみて見やすい方を選んでみてください。
JavaScriptはWebPackなりでバンドルされて実行されるのがほとんどで、このくらいなら実行速度に影響ないので、その時々で見やすいと思える方で良いと思います🙆

Comment on lines +17 to +18
const num = nums[i]
const pair_num = target - num

Choose a reason for hiding this comment

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

これくらいだったら17行目の定義は必要ないのかなと思いました、好みの話かもしれませんが(javascriptをあまり把握していないのでもしこれが必要で合ったらスルーしてください)

Copy link

Choose a reason for hiding this comment

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

const pair_num = target - nums[i]
くらいで良いとは思いますね。
あと見落としてましたがJS/TSだと変数はlowerCamelCaseで書くのが普通です。

https://google.github.io/styleguide/jsguide.html#naming-local-variable-names

Copy link
Owner Author

Choose a reason for hiding this comment

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

これくらいだったら17行目の定義は必要ないのかなと思いました

const pair_num = target - nums[i]
くらいで良いとは思いますね。

ありがとうございます。
確かにその通りだとも思いました。

あと見落としてましたがJS/TSだと変数はlowerCamelCaseで書くのが普通です。

変数名ですが、以前同じ指摘をodaさんから受けていました。
指摘いただき、ありがとうございます。


## STEP2

* 添え字を、iではなく、idxに変更。

Choose a reason for hiding this comment

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

こちら、特にどちらでも良いかなと思うのですが何か意図や、参考にした記事があるのでしょうか?

Copy link
Owner Author

@syoshida20 syoshida20 May 10, 2025

Choose a reason for hiding this comment

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

参考にした記事は特にありません。

2つのindexを返す関数において、
返り値の片方がiで、もう片方がpair_idxであるのが、違和感があったためです。

https://github.com/shintaroyoshida20/leetcode/blob/402afc125c54bc677cbda32a2f19998dda73b728/hash-map/two-sum/answer.md#L36-L44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants