-
Notifications
You must be signed in to change notification settings - Fork 0
1. Two Sum #3
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
1. Two Sum #3
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,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] }; | ||
} | ||
}; |
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] }; | ||
} | ||
}; |
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]; | ||
if(numMap.find(currentOther) != numMap.end()){ | ||
break; | ||
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. (小田さんのコメントと重複しますが)ここでreturnしてあげれば、 |
||
} | ||
numMap[nums[index]] = index; | ||
} | ||
|
||
return { index, numMap[currentOther] }; | ||
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. (これも小田さんのコメントと重複しますが)課題制約で解があることは保証されていますが、解がない場合も考慮してみるとどうでしょうか。 |
||
} | ||
}; |
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 {}; | ||
} | ||
}; |
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.
もっといい命名がある気がします。「現在の他」とはどういうことだろうと思いました。
pair
、complement
、numToFind
あたりでしょうか?もっと良いのがあるかもしれないですが。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.
回答となる組を作るイメージで、
nums[i]
をcurrentでその片方という意味でcurrentOther
としてましたがnumToFind
のほうがわかりやすいですね