-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| numsの値とindexをmapにindexの方はvectorで入れて、値の2倍がtargetになっているときはvectorの最初の二つのindexを返す、そうでないときはそれぞれのindexの最初のものを返すというやり方を思いついたのでそれで書いた。 | ||
|
|
||
| ### step2 | ||
|
|
||
| unordered_mapと変数xの名前をより具体的な名前に修正した。 | ||
|
|
||
| ### step3 | ||
|
|
||
| unordered_mapにvectorを入れなくていいシンプルな方法をChatGPTに提示されたのでそれで書き直して、3回正解するまでやり直した。 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| unordered_map<int, vector<int>> map; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 型名をそのまま変数名に含めても、読み手にとって得られる情報はあまり増えないように感じました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます!num_to_indicesのような名前をつけるよう心がけます。 |
||
| for (int i = 0; i < nums.size(); i++) { | ||
| map[nums[i]].push_back(i); | ||
| } | ||
| vector<int> retVal; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一般に変数のスコープは短ければ短いほど良いと思います。これは、変数のスコープを短くすることによって、読み手の短期記憶の容量を節約することができる多mです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. retVal という変数にはあまり情報がないように思いました。自分なら indices や、冗長に書くなら indices_of_two_numbers と付けると思います。 |
||
| for (auto it = map.begin(); it != map.end(); it++) { | ||
| int x = it->first; | ||
| if (2 * x == target && it->second.size() >= 2) { | ||
| retVal.push_back(it->second[0]); | ||
| retVal.push_back(it->second[1]); | ||
| return retVal; | ||
| } else if (2 * x != target) { | ||
|
Comment on lines
+11
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 * x と target の関係を2回書いていますが、 if (2 * x == target) {
if (it->second.size() >= 2) {
...
}
} else {
...
}という気持ちではないですかね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. if (2 * x == target && it->second.size() >= 2) {
...
return retVal;
}
if (2 * x != target) {
...
}こちらもわりと自然な気持ちとは思います。考え方次第です。いくつかの自明な変形を常に頭においておきましょう。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 承知しました!常にいくつかの選択肢から取捨選択できるようにしたいと思います。 |
||
| auto it2 = map.find(target - x); | ||
| if (it2 != map.end()) { | ||
| retVal.push_back(it->second[0]); | ||
| retVal.push_back(it2->second[0]); | ||
| return retVal; | ||
| } | ||
| } | ||
| } | ||
| return {}; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| unordered_map<int, vector<int>> num_index; | ||
| for (int i = 0; i < nums.size(); i++) { | ||
| num_index[nums[i]].push_back(i); | ||
| } | ||
| vector<int> retVal; | ||
| for (auto it = num_index.begin(); it != num_index.end(); it++) { | ||
| int first_num = it->first; | ||
| if (2 * first_num == target && it->second.size() >= 2) { | ||
| retVal.push_back(it->second[0]); | ||
| retVal.push_back(it->second[1]); | ||
| return retVal; | ||
| } else if (2 * first_num != target) { | ||
| auto it2 = num_index.find(target - first_num); | ||
| if (it2 != num_index.end()) { | ||
| retVal.push_back(it->second[0]); | ||
| retVal.push_back(it2->second[0]); | ||
| return retVal; | ||
| } | ||
| } | ||
| } | ||
| return {}; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> twoSum(vector<int>& nums, int target) { | ||
| unordered_map<int, int> value_index; | ||
| for (int i = 0; i < nums.size(); i++) { | ||
| int another = target - nums[i]; | ||
| if (value_index.count(another)) { | ||
| return { value_index[another], i }; | ||
| } | ||
| value_index[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.
発想方法として手作業でやることを考えてほしいかなと思います。(なんとなくできてそうですが。)
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.7n6wwffw10hb
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.z4zz4wpn0zz0
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.
承知しました!まずは手作業でやれることを意識して考えるようにします。