Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

two sum O(n) leetcode

//O(n) hashmaps C++ Two Sum leetcode 
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> map;
        for(int i=0; i<=nums.size();i++){
            int compliment=target-nums[i];
            if(map.count(compliment)==1){
                return {map[compliment],i};
            }
            else{
                map[nums[i]]=i;
            }
            
     	}
        return {-1,-1};
        
    }
};
 
PREVIOUS NEXT
Tagged: #sum #leetcode
ADD COMMENT
Topic
Name
9+3 =