Advertisement
Zagoshipda

2sums c++

Jan 29th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define endl '\n'
  6. #define ll long long    // range : -9*10^18 ~ 9*10^18
  7.  
  8.  
  9.  
  10. class Solution {
  11. public:
  12.     vector<int> twoSum(vector<int>& nums, int target) {
  13.         unordered_set<int> s;
  14.         vector<int> ans;
  15.         for(auto e : nums){
  16.             cout << e << endl;
  17.         }
  18.         for(int i=0; i<nums.size(); i++){
  19.             auto search = s.find(nums[i]);
  20.             if(search != s.end()){
  21.                 ans.push_back(nums[i]);
  22.                 ans.push_back(*search);
  23.                 break;
  24.             }else{
  25.                 s.insert(nums[i]);
  26.             }
  27.         }
  28.         for(auto e : s){
  29.             cout << e << endl;
  30.         }
  31.         return ans;
  32.     }
  33.     void prac(){
  34.         cout << "hello world" << endl;
  35.     }
  36. };
  37.  
  38. int main(){
  39.     IOS;
  40.  
  41.     Solution sol = Solution();
  42.     vector<int> input {2, 7, 11, 15};
  43.     int target = 9;
  44.  
  45.     vector<int> ans = sol.twoSum(input, target);
  46.     cout << ans[0] << ' ' << ans[1];
  47.  
  48.     // sol.prac();
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement