// o(n) 的复杂度
#include<vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int len = numbers.size();
vector<int> index;
int j = len-1;
int i = 0;
while(numbers[i]<=numbers[j] && i!=j){
if(numbers[i]+numbers[j] == target){
index.push_back(i+1);
index.push_back(j+1);
break;
}
if(numbers[i]+numbers[j] < target)
i++;
if(numbers[i]+numbers[j] > target)
j--;
}
return index;
}
};
47 - 算法 - Leetcode -167-两数之和 - 输入有序数组
最新推荐文章于 2024-08-20 09:08:03 发布