-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #17
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
base: main
Are you sure you want to change the base?
1. Two Sum #17
Conversation
2b1aca6 to
edaa491
Compare
| } | ||
| num_to_index_table.set(num, i) | ||
| } | ||
| return new Error(`The pair is not found for the target value (${value}).`) |
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.
JavaScript は throw じゃなかったでしたっけ。
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.
実行が中断されることを期待していたのですが、
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.
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.
402afc1 でレビューを反映したコードを追加しました。
| }; | ||
| ``` | ||
|
|
||
| * mapをもっと綺麗にかける。 |
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.
誤ってるかは微妙ですが、、、適当な長さでいくつか書いてみて見やすい方を選んでみてください。
JavaScriptはWebPackなりでバンドルされて実行されるのがほとんどで、このくらいなら実行速度に影響ないので、その時々で見やすいと思える方で良いと思います🙆
| const num = nums[i] | ||
| const pair_num = target - num |
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.
これくらいだったら17行目の定義は必要ないのかなと思いました、好みの話かもしれませんが(javascriptをあまり把握していないのでもしこれが必要で合ったらスルーしてください)
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.
const pair_num = target - nums[i]
くらいで良いとは思いますね。
あと見落としてましたがJS/TSだと変数はlowerCamelCaseで書くのが普通です。
https://google.github.io/styleguide/jsguide.html#naming-local-variable-names
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.
これくらいだったら17行目の定義は必要ないのかなと思いました
const pair_num = target - nums[i]
くらいで良いとは思いますね。
ありがとうございます。
確かにその通りだとも思いました。
あと見落としてましたがJS/TSだと変数はlowerCamelCaseで書くのが普通です。
変数名ですが、以前同じ指摘をodaさんから受けていました。
指摘いただき、ありがとうございます。
|
|
||
| ## STEP2 | ||
|
|
||
| * 添え字を、iではなく、idxに変更。 |
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.
参考にした記事は特にありません。
2つのindexを返す関数において、
返り値の片方がiで、もう片方がpair_idxであるのが、違和感があったためです。
問題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:
Example 2:
Example 3:
Constraints:
Follow-up
Can you come up with an algorithm that is less than O(n2) time complexity?