赞
踩
思路
先排序,在查找
-
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- vector<pair<int,int>> total;
- for(int i=0;i<nums.size();i++){
- total.push_back({nums[i],i});
- }
- sort(total.begin(),total.end());
- vector<int> res(2);
- int start=0,end=nums.size()-1;
- while(start<end){
- if(total[start].first+total[end].first==target){
- res[0]=total[start].second;res[1]=total[end].second;
- return res;
- }
- else if(total[start].first+total[end].first>target)
- end--;
- else
- start++;
- }
- return res;
- }
- };

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。