-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #10
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 #10
Conversation
| std::vector<int> twoSum(std::vector<int>& nums, int target) { | ||
| std::unordered_map<int, int> num_to_index; | ||
|
|
||
| for (int i = 0; i < (int)nums.size(); ++i) { |
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.
C++スタイルのstatic_cast(nums.size())とする方がいいと思います。
これはキャストの見つけやすさとキャストの意図が明確になることからMore Effective c++の第二項で推奨されておりました。
| ```C++ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <unordered_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.
細かいですが、includeはアルファベット順がいいと思いました。
Within each section the includes should be ordered alphabetically.
https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes
|
|
||
| for (int i = 0; i < (int)nums.size(); ++i) { | ||
| int complement = target - nums[i]; | ||
| if (num_to_index.find(complement) != num_to_index.end()) { |
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にはcontainsというものがございます。
if (num_to_index.contains(complement))と書くこともできます。
https://en.cppreference.com/w/cpp/container/unordered_map/contains.html
1_two_sum/memo.md
Outdated
| return 0; | ||
| } | ||
| ``` | ||
| やっぱ、unordered_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.
unordered_mapはkeyとvalueを持った構造だと思います。
unordered_mapの動きを理解するためにforループの各段階で、keyとvalueには何が入っているのか紙に書き出してみるのは如何でしょうか。
| hash tableらしい。 | ||
| 今回使用しているのは、intを入れてintを返すモノかな。 | ||
| うーん、いまいちイメージがつかない。 |
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は、ハッシュマップなのでPythonでいうdict(辞書)ですね。今回はint型とint型を組にしているので、たまたま「intを入れてintを返す」動作になっています。
| for (int i = 0; i < (int)nums.size(); ++i) { | ||
| int complement = target - nums[i]; | ||
| if (num_to_index.find(complement) != num_to_index.end()) { | ||
| return {num_to_index[complement], i}; | ||
| } | ||
| num_to_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.
好みの問題ですが、個人的には範囲for文の方が読みやすいです。
int i = 0;
for (const auto& num : nums) {
int complement = target - num;
if (num_to_index.find(complement) != num_to_index.end()) {
return {num_to_index[complement], i};
}
num_to_index[num] = i;
i++;
}
return {};| std::unordered_map<int, int> num_to_index; | ||
| for (int i = 0; i < (int)nums.size(); ++i) { | ||
| int complement = target - nums[i]; | ||
| if (num_to_index.find(complement) != num_to_index.end()) { | ||
| return {num_to_index[complement], i}; | ||
| } | ||
| num_to_index[nums[i]] = i; |
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のkeyに相当するのは,今までの数字の中でnums[i]を足すとtargetになる数(complement)だと思うのでnum_to_indexよりはcomp_to_indexなどのほうが適切かもしれませんね
| return 0; | ||
| } | ||
| ``` | ||
| やっぱ、unordered_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.
C で関数を呼ぶとき、関数の中身を把握するというレイヤーと関数を呼んだら何が起きるかというレイヤーの2つがあると思うんですが、データ構造も、どういう中身なのかと、外から見たらどういうものなのかという2つの層があるように思います。まずは、外から見たらどういうものかを考えてみたらよいかと思います。
This Problem
https://leetcode.com/problems/two-sum/description
Next Problem
https://leetcode.com/problems/add-two-numbers/description