---
# System prepended metadata

title: 'LeetCode : 0001. Two Sum (array)'
tags: [leetcode]

---

# LeetCode : 0001. Two Sum (array)
###### tags:`leetcode`
```
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <unordered_map>
using std::vector;
using std::unordered_map;
using namespace std;

class BaseVectorPrint {
    public:
        void BasePrint00(vector<int>& num) {
            for (size_t i = 0; i < num.size(); ++i) {
                cout << num.at(i) << "; ";
            }
            cout << endl;
        }

        void BasePrint01(vector<int>& num) {
            for (size_t i = 0; i < num.size(); ++i) {
                cout << num[i] << "; ";
            }
            cout << endl;
        }

        void BasePrint02(vector<int>& num) {
            for (const auto &item : num) {
                cout << item << "; ";
            }
            cout << endl;
        }
        
        void TwoDimensionalPrint(vector<vector<int>> &num){
            for (int i = 0; i < num.size(); i++)
            {
                for (int j = 0; j < num[i].size(); j++)
                {
                    cout << "[" << num[i][j] << "]";
                }
                cout << endl;
            }
        }
};


class BaseSolution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        int numsLen = nums.size();

        for(int i = 0; i<numsLen;i++){
            int findnums = target - nums[i];
                for( int j = i+1; j<numsLen;j++){
                    if(findnums == nums[j]){
                        ans.push_back(i);
                        ans.push_back(j);
                    }
                }
        }
        return ans;
    }
};


class Solution2 {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hashmap;
        
        for (int i = 0;; ++i) {
            int findnums = target - nums[i];
            //auto it = hashmap.find(target - nums[i]);
            auto it = hashmap.find(findnums);
            // hashmap.find() , if can't find Value return Null (0);

            //hashmap.end() == null;
            if (it != hashmap.end()) 
                return vector<int> {it->second, i};
                //return vector<int> {i, it->second};
                
            hashmap[nums[i]] = i;
            // hashmap input hashmap[key] = value;
        }
    }
};

int main(void)
{
    BaseVectorPrint printVector;
    Solution2 two;

    vector<int> test = {2,7,11,15};
    vector<int> ans ;

    ans = two.twoSum(test,9);
    printVector.BasePrint00(ans);

    //printf("%d\n",input_Nums);
    printf("test\n");
    system("pause");
}

```