class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//两个数的和
vector<int> numss=nums;
sort(nums.begin(),nums.end());
int len=nums.size();
int first=0;
int last=len-1;
vector<int> result;
while(first!=last)
{
if(nums[first]+nums[last]>target)
--last;
else if(nums[first]+nums[last]<target)
++first;
else
{
result.push_back(nums[first]);
result.push_back(nums[last]);
break;
}
}
vector<int> result_n;
vector<int> flag(len);
for(int j=0;j<result.size();++j)
{
for(int i=0;i<len;++i)
{
if(numss[i]==result[j]&&flag[i]!=1)
{
result_n.push_back(i);
flag[i]=1;
break;
}
}
}
return result_n;
}
};
LeetCode-1 两数之和
最新推荐文章于 2024-05-16 01:00:00 发布