双指针
vector<int> twoSum(vector<int>& nums, int target) {
// 题目中说了这是一个递增数组,而且我需要两个数字组成s
vector<int> res;
int smallDigit = 0, bigDigit = nums.size() - 1;
// 这要结果存在,这两个指针就不会相等
// 也不能相等,相等就是同一个数字用两遍
while (smallDigit < bigDigit) {
if ((nums[smallDigit] + nums[bigDigit]) == target) {
res = { nums[smallDigit],nums[bigDigit] };
break;
}
else if ((nums[smallDigit] + nums[bigDigit]) < target) {
smallDigit++;
}
else bigDigit--;
}
return res;
}
标签:bigDigit,数字,nums,res,57,Offer,vector,smallDigit,target
From: https://www.cnblogs.com/yaocy/p/17622500.html