jayati

2sum (O(n))

Sep 13th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. int sum=target;
  5. vector<int> v;
  6. int n=nums.size();
  7. unordered_map<int,int> map;
  8.  
  9. for(int index = 0; index < nums.size(); index++){
  10. if(map.find(target-nums[index]) != map.end()){
  11.  
  12. return {index, map[target-nums[index]]};
  13. }
  14. map[nums[index]] = index;
  15.  
  16. }
  17.  
  18.  
  19. return {};
  20. }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment