PS/LeetCode
[LeetCode] 001 Two Sum
시옷지읏
2021. 4. 20. 12:01
leetcode.com/problems/two-sum/
Two Sum - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
처음 iterator를 썼다가, Edge Case를 해결하지 못했다. {3,3} 일때 0,0이 나왔음.
#include <vecotor>
#include <map>
using namespace std;
class solution{
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> m;
for(int i=0; i<nums.size(); ++i){
if( m.find(target-nums.at(i)) != m.end() ){
return { i, m[target-nums[i] ] };
}
m.emplace(nums[i], i);
}
return {};
};