๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Two Sum

by dkswnkk 2021. 10. 23.

 

 

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

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int>ans;
        unordered_map<int,int>m;

        for(int i=0; i<nums.size(); i++){
            int curr=target-nums[i];
            auto it=m.find(curr);
            if (it != m.end()){
                ans.push_back(m[curr]);
                ans.push_back(i);
                return ans;
            } 
            else m[nums[i]] = i;
        }
      return ans;
    }
};

๋Œ“๊ธ€