Skip to content
Merged
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
19 changes: 19 additions & 0 deletions 1/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> numMap;

int index = 0;
int currentTargetNum = 0;
for(; index < nums.size(); index++){
currentTargetNum = target - nums[index];
if(numMap.find(currentTargetNum) != numMap.end() && index != numMap[currentTargetNum]){
break;
}

numMap[nums[index]] = index;
}

return { index, numMap[currentTargetNum] };
}
};
18 changes: 18 additions & 0 deletions 1/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> numMap;

int index, currentTargetNum;
for(index = 0; index < nums.size(); index++){
currentTargetNum = target - nums[index];
if(numMap.find(currentTargetNum) != numMap.end()){
break;
}

numMap[nums[index]] = index;
}

return { index, numMap[currentTargetNum] };
}
};
17 changes: 17 additions & 0 deletions 1/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> numMap;
int currentOther, index;

for(index = 0; index < nums.size(); index++){
currentOther = target - nums[index];

Choose a reason for hiding this comment

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

もっといい命名がある気がします。「現在の他」とはどういうことだろうと思いました。paircomplementnumToFindあたりでしょうか?もっと良いのがあるかもしれないですが。

Copy link
Owner Author

Choose a reason for hiding this comment

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

回答となる組を作るイメージで、nums[i]をcurrentでその片方という意味でcurrentOtherとしてましたが
numToFindのほうがわかりやすいですね

if(numMap.find(currentOther) != numMap.end()){
break;

Choose a reason for hiding this comment

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

(小田さんのコメントと重複しますが)ここでreturnしてあげれば、int currentOther, indexのスコープをfor文に限定できます。これくらいのコードであれば、変数の数も少ないのでスコープを限定しなくても書けちゃいますが、ある程度複雑になると不必要に脳内のワーキングメモリを使うことになります。

}
numMap[nums[index]] = index;
}

return { index, numMap[currentOther] };

Choose a reason for hiding this comment

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

(これも小田さんのコメントと重複しますが)課題制約で解があることは保証されていますが、解がない場合も考慮してみるとどうでしょうか。

}
};
15 changes: 15 additions & 0 deletions 1/4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> numMap;
for(int index = 0; index < nums.size(); index++){
int numToFind = target - nums[index];
if(numMap.find(numToFind) != numMap.end()){
return { index, numMap[numToFind] };
}
numMap[nums[index]] = index;
}

return {};
}
};