---
# System prepended metadata

title: 0001. Two Sum (Easy)
tags: [LeetCode, Sum subarray related problems]

---

###### tags: `LeetCode`, `Sum subarray related problems`

# 0001. Two Sum (Easy)
耗時：? 分鐘

## 題目
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

## 思路
1. 暴力解
2. (考慮解法) two pointer，應該能O(n)

Time Complexity:O(N^2)
### 程式碼
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        
        for(int i = 0; i < nums.size(); ++i)
        {
            for (int j = i + 1; j < nums.size(); ++j)
            {
                if (nums[i] + nums[j] == target)
                {
                    ans.push_back(i);
                    ans.push_back(j);
                    return ans;
                }
            }
        }
        
        return ans;
    }