int* twoSum(int* nums, int numsSize, int target, int* returnSize){ hashtable = NULL; for (int i = 0; i < numsSize; i++) { structhashTable* it = find(target - nums[i]); if (it != NULL) { int* ret = malloc(sizeof(int) * 2); ret[0] = it->val, ret[1] = i; *returnSize = 2; return ret; } insert(nums[i], i); } *returnSize = 0; returnNULL; }
1 2 3 4 5 6 7 8
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: hashtable = dict() for i, num inenumerate(nums): if target - num in hashtable: return [hashtable[target - num], i] hashtable[nums[i]] = i return []
1 2 3 4 5 6 7 8
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: hashtable = dict() for i, num in enumerate(nums): if target - num in hashtable: return [hashtable[target - num], i] hashtable[nums[i]] = i return []