题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
使用了两层循环:
class Solution { public: /** * * @param numbers int整型vector * @param target int整型 * @return int整型vector */ vector<int> twoSum(vector<int>& numbers, int target) { // write code here // 最简单的应该是 o(n^2) 了 int i = 0; int j = 1; for(;i<numbers.size() - 1; ++i){ for(j = i+1;j<numbers.size(); ++j){ if(numbers[i] + numbers[j] == target) { return {i+1,j+1}; } } } return {i+1,j+1}; } };