Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0001-two-sum/memo.md
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回正解するまでやり直した。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

承知しました!まずは手作業でやれることを意識して考えるようにします。

26 changes: 26 additions & 0 deletions 0001-two-sum/step1.cpp
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

型名をそのまま変数名に含めても、読み手にとって得られる情報はあまり増えないように感じました。
変数名には、どのような値が格納されているかが分かる名前を付けると、より読みやすくなると思います。特に map や unordered_map の場合は、キーと値にどのような値が含まれているかを変数名で示すとよいと思います。自分はよく (キー)_to_(値) という命名をします。今回の場合は num_to_indices はいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一般に変数のスコープは短ければ短いほど良いと思います。これは、変数のスコープを短くすることによって、読み手の短期記憶の容量を節約することができる多mです。
retVal は、以下の for 文の中でしか使っていないため、 for 文の中、特に使用する直前で定義するとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

これはおっしゃる通りですね。変数のスコープも意識するようにしてできるだけ短くするようにします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 {
    ...
}

という気持ちではないですかね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!その気持ちでした。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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) {
    ...
}

こちらもわりと自然な気持ちとは思います。考え方次第です。いくつかの自明な変形を常に頭においておきましょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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 {};
}
};
26 changes: 26 additions & 0 deletions 0001-two-sum/step2.cpp
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 {};
}
};
14 changes: 14 additions & 0 deletions 0001-two-sum/step3.cpp
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 {};
}
};