-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #1
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 #1
Conversation
webhook test comment |
ans.push_back(i); | ||
ans.push_back(j); | ||
break ; |
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.
シンプルに return {i, j};
でも良さそうです。
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.
このbreakのみだと、外側のループから抜けられていないですね。
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.
breakは全部のループ抜けるものだと勘違いしていました。。。
unordered_map<int, int> mp; | ||
|
||
for (int i = 0; i < nums.size(); i++) { | ||
if (mp.count(target - nums[i]) > 0) { |
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.
unordered_map<int, int>::find
を使ってもいいですね。
https://en.cppreference.com/w/cpp/container/unordered_map/find
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.
他にもcontainsを使えばシンプルに書けると思いました。
https://en.cppreference.com/w/cpp/container/unordered_map/contains
} | ||
numsMap[nums[i]] = i; | ||
} | ||
return {}; |
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.
適切な引数が渡されればここはunreachableなので、選択肢としては例外を投げてもいいですね。
参考 https://discord.com/channels/1084280443945353267/1218740927120674977/1223967037588635658
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.
他の選択肢としては、空配列を返しつつエラーログを吐くという手もあります
どういう選択肢を取るかはこの関数が誰のためにどのような場面で使われるか(ユースケース)によって変わってくるので、なにか前提があって書いたときはコメントに残しておくと良さそうです
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> numsMap; |
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.
命名はもう少し工夫しても良いかなと思います。mapということは型を見れば分かるので実質変数名の情報がnums
しかなくて引数のnums
と何が違うんだろうと思いました。
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> numsMap; |
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.
補数( と思うのでそれを表現した方がいいかもしれませんcomplement
)とindex
の対応を取ってる
あ、補数はこのmapにいれてないや
失礼しました
unordered_map<int, int> numsMap; | ||
|
||
for(int i = 0; i < nums.size(); i++) { | ||
if (numsMap.count(target - nums[i]) > 0) { |
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.
containsでも良さそうです
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.
このようにもできます。
if (auto it = numsMap.find(target - nums[i]); it != numsMap.end()) {
return {it->second, i};
}
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> mp; |
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.
変数名 mp
ですと、中にどのような値が含まれているか想像しにくく感じます。 num とその index がはいっていますので、 num_to_index
といった名前はいかがでしょうか?
class Solution { | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) { | ||
unordered_map<int, int> numsMap; |
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.
念のためですが、googleのスタイルガイドではスネークケースが推奨されております。
https://google.github.io/styleguide/cppguide.html#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.
@hayashi-ay さんも指摘されておりますが、変数名を見て何をいれているのかわかる命名がいいと思います。mapは型定義から分かるので変数名に加える必要はないと思いました。
https://leetcode.com/problems/two-sum/description