牛客题霸--两数之和题解
两数之和
http://www.nowcoder.com/questionTerminal/20ef0972485e41019e39543e8e895b7f
暴力搜索一下, 时间复杂度 O( 小于 n^2)
class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int>vec; for ( int i = 0; i < numbers.size(); i++ ) { for ( int j = i + 1; j < numbers.size(); j++ ) { if ( numbers[i] + numbers[j] == target) { vec.push_back(i + 1); vec.push_back(j + 1); break; } } } return vec; } };