class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int n = nums.size(); vector<int>idxs; for(int i = 0;i<n;i++) idxs.push_back(i); sort(idxs.begin(),idxs.end(),[nums,idxs](int i,int j){return nums[idxs[i]]<nums[idxs[j]];}); int l = 0,r = n - 1; vector<int> result; while(l < r){ int sum = nums[idxs[l]] + nums[idxs[r]]; if(sum == target){ result.push_back(idxs[l]); result.push_back(idxs[r]); return result; }else if(sum < target){ l++; }else if(sum > target) { r--; } } return result; } };
标签:target,nums,int,vector,result,两数 From: https://www.cnblogs.com/hbro/p/17398811.html