首页 > 代码库 > Two Sum

Two Sum

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
       vector<int>result;
       vector<int>::iterator p;
       vector<int>::iterator num=nums.begin();
       for(int i=0;i<nums.size();i++)
       {
           int flag=target-nums[i];
           p=find(nums.begin()+i+1,nums.end(),flag);
           if(p!=nums.end())
           {
               int indices=p-num;
               result.push_back(i);
               result.push_back(indices);
           }
       }
        return result;
    }
};


本文出自 “算法设计” 博客,请务必保留此出处http://shineprince.blog.51cto.com/10293320/1913894

Two Sum