본문 바로가기
Coding Test/LeetCode

[LeetCode] Two Sum

by song.ift 2024. 10. 29.

https://leetcode.com/problems/two-sum/

 


class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> m;
        
        for (int i = 0; i < nums.size(); ++i)
        {
            int n = target - nums[i];
            
            if (m.find(n) != m.end() && i != m[n]) return { i, m[n] };
            else m.insert({nums[i], i});
        } 
        return {0, 0};
    }
};

GitHub : https://github.com/developeSHG/Algorithm-LeetCode/commit/0cfd8d69436f395f4246fdac3f9c019f161802fc

 

Time: 4 ms (66.69%), Space: 14.4 MB (20.56%) - LeetHub · developeSHG/Algorithm-LeetCode@0cfd8d6

developeSHG committed Oct 29, 2024

github.com

 

댓글