Two Sum

本来想排个序,直接搜索就过了,复杂度O(n*n)。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res(2,0);
        //sort(nums.begin(), nums.end());
        int length = int(nums.size());
        int tmp = 0;
        for (int i=0; i<length; i++) {
            tmp = target-nums[i];
            for (int j=i+1; j<length; j++) {
                if (nums[j]==tmp) {
                    res[0]=i;
                    res[1]=j;
                    return res;
                }
            }
        }
        return res;
    }
};

参考网站:1、题目网站:https://leetcode.com/problems/two-sum/

2、vector操作网站:http://blog.csdn.net/phoebin/article/details/3864590


你可能感兴趣的:(SUM,LeeCode,Two)