0

This solution to the problem presented on https://leetcode.com/problems/two-sum/:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2

by wz2326 (https://leetcode.com/discuss/58175/clean-16ms-c-solution):

vector<int> twoSum(vector<int>& nums, int target) {
    unordered_map<int, int> hash;
    vector<int> res(2, 0);
    for (int i = 0; i < nums.size(); i++) {
        if (hash.find(target - nums[i]) != hash.end()) {
            res[0] = hash[target - nums[i]], res[1] = i + 1;
            return res;
        }
        hash[nums[i]] = i + 1;
    }
}

introduces pretty strange use of commas during assignment of int values to vector elements:

res[0] = hash[target - nums[i]], res[1] = i + 1;

Does anyone know the meaning of comma here? Thanks.

Anon
  • 1,274
  • 4
  • 17
  • 44

0 Answers0