Skip to content

Conversation

@jiku0730
Copy link
Owner

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) {

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>

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()) {

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

return 0;
}
```
やっぱ、unordered_mapの使い方がわからない。

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には何が入っているのか紙に書き出してみるのは如何でしょうか。

Comment on lines +87 to +89
hash tableらしい。
今回使用しているのは、intを入れてintを返すモノかな。
うーん、いまいちイメージがつかない。
Copy link

@skypenguins skypenguins Jun 12, 2025

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を返す」動作になっています。

Comment on lines +19 to +26
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 {};

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 {};

Comment on lines +6 to +12
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;

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の使い方がわからない...
Copy link

Choose a reason for hiding this comment

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

C で関数を呼ぶとき、関数の中身を把握するというレイヤーと関数を呼んだら何が起きるかというレイヤーの2つがあると思うんですが、データ構造も、どういう中身なのかと、外から見たらどういうものなのかという2つの層があるように思います。まずは、外から見たらどういうものかを考えてみたらよいかと思います。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants