Clean C++ solution


  • 9
    class Solution {
    public:
        vector<int> twoSum(vector<int> &nums, int target) {
            unordered_map<int, int> record;
    
            for (auto i = 0; i < nums.size(); ++i) {
                auto item = record.find(target - nums[i]);
                if (item != record.end()) {
                    return {item->second, i};
                }
    
                record[nums[i]] = i;
            }
        }
    };

  • 1
    E

    Please test your code on this input: [ 5, 1 , 2 , 3, 5], target 10


  • 0

    It's OK. Is there something wrong?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.