Two Sum
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { std::unordered_map<int,int> remainings; int size=nums.size(); vector<int> solution; for(int i=0;i<size;i++) { int remaining=target-nums[i]; if(remainings.contains(nums[i])) { return {remainings[nums[i]],i}; } remainings[remaining]=i; } return {}; } };
Leave a Comment