力扣-数组-01两数之和
解析
遍历i和第i+1,两个for循环查就可以,时间复杂度是
代码
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ansewer;
bool flag = false;
for(int i = 0;i < nums.size(); i++){
for(int j = i+1;j < nums.size(); j++){
if(nums[i] + nums[j] == target){
ansewer.push_back(i);
ansewer.push_back(j);
flag = true;
break;
}
}
if(flag)
break;
}
return ansewer;
}
};