Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

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

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
	vector<int> towSum(vector<int>& nums, int target) {
		vector<int> indices;
		int index1 = 0;
		int index2 = 0;
		for (int i = 0; i < nums.size() - 1; i++) {
			int number = nums[i];
			for (int j = i + 1; j < nums.size(); j++) {
				if (number + nums[j] == target) {
					index1 = i;
					index2 = j;
					break;
				}
			}
		}
		indices.push_back(index1);
		indices.push_back(index2);
		return indices;
	}
};

 

上一篇:Oracle 密码包含特殊字符OTL访问的解决方案


下一篇:四、GAMIT解算之网平差