
Preface
This article carries a strong personal flavor. If it makes you uncomfortable, please close it as soon as possible. This article is for personal study notes only. You’re welcome to repost or share it within the bounds of the license agreement — please respect the copyright and keep the original link. Thank you for your understanding and cooperation. If you find this site helpful, you can subscribe via RSS. Thanks for your support!
Problem Statement
Given an array of integers nums and an integer target value target, find the two integers in the array whose sum equals the target value target, and return their array indices.
You may assume that each input would have exactly one solution. However, the same element in the array may not appear twice in the answer.
You may return the answer in any order.
Example 1
1
2
3
4
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
Example 2
1
2
输入:nums = [3,2,4], target = 6
输出:[1,2]
Example 3
1
2
输入:nums = [3,3], target = 6
输出:[0,1]
Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
for (int j = i+1; j < n; ++j) {
if (nums[i] + nums[j] == target) {
return {i,j};
}
}
}
return {};
}
};