LeetCode:两数之和

C++示例程序:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int length = nums.size();
        vector<int> result;
        unordered_map<int, int> um;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (nums[i] + nums[j] == target) {
                    result.push_back(i);
                    result.push_back(j);
                }
            }    
        }
        return result;
    }
};

猜你喜欢

转载自www.cnblogs.com/yiluyisha/p/9278528.html