배움 저장소

[LeetCode] 001 Two Sum 본문

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

 

'PS > LeetCode' 카테고리의 다른 글

[Leet Code]055 Jump Game  (0) 2021.10.06
[Leet Code]009 Palindorme Number  (0) 2021.05.21
[Leet Code]007 Reverse Integer  (0) 2021.05.12
[Leet Code] 006 ZigZag Conversion  (0) 2021.05.10
[LeetCode] 002 Add Two Numbers  (0) 2021.04.20
Comments