classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: for i inrange(len(nums)): for j inrange(i+1,len(nums)): if nums[i]+nums[j]==target: return [i,j]
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: a = {} for i inrange(0,len(nums)): if target-nums[i] in a: return [a[target-nums[i]],i] else: a[nums[i]] = i
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: vector<int> twoSum(vector<int>& nums, int target){ unordered_map<int,int> hashmap; for(int i = 0; i < nums.size(); i++){ auto h = hashmap.find(target-nums[i]); if(h != hashmap.end()){ return {hashmap[target-nums[i]],i}; }else{ hashmap[nums[i]] = i; } } return {}; } };