classSolution { public: vector<int> twoSum(vector<int>& nums, int target){ unordered_map<int, int> hash_map; for (int i = 0; i < nums.size(); ++i) { int complement = target - nums[i]; if (hash_map.find(complement) != hash_map.end()) { return {hash_map[complement], i}; } hash_map[nums[i]] = i; } return {}; } };
结果
执行用时 : 12 ms, 击败 70.06% 使用 C++ 的用户
内存消耗 : 10.2 MB, 击败 0.82% 使用 C++ 的用户
Java
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicint[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> map = newHashMap<>(); for (inti=0; i < nums.length; i++) { intcomplement= target - nums[i]; if (map.containsKey(complement)) { returnnewint[]{map.get(complement), i}; } map.put(nums[i], i); } returnnewint[]{}; } }
结果
执行用时 : 1 ms, 击败 99.50% 使用 Java 的用户
内存消耗 : 43.02 MB, 击败 5.10% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): deftwoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ num_indices = {} for i, num inenumerate(nums): complement = target - num if complement in num_indices: return [num_indices[complement], i] num_indices[num] = i return []
结果
执行用时 : 24 ms, 击败 78.55% 使用 Python 的用户
内存消耗 : 13.66 MB, 击败 65.80% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9
classSolution: deftwoSum(self, nums: List[int], target: int) -> List[int]: num_indices = {} for i, num inenumerate(nums): complement = target - num if complement in num_indices: return [num_indices[complement], i] num_indices[num] = i return []
结果
执行用时 : 40 ms, 击败 92.95% 使用 Python3 的用户
内存消耗 : 17.39 MB, 击败 13.79% 使用 Python3 的用户
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* nums, int numsSize, int target, int* returnSize) { int* result = (int*)malloc(2 * sizeof(int)); *returnSize = 0; for (int i = 0; i < numsSize - 1; i++) { for (int j = i + 1; j < numsSize; j++) { if (nums[i] + nums[j] == target) { result[0] = i; result[1] = j; *returnSize = 2; return result; } } } free(result); returnNULL; }
结果
执行用时 : 120 ms, 击败 67.82% 使用 C 的用户
内存消耗 : 6.93 MB, 击败 49.60% 使用 C 的用户
C#
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { publicint[] TwoSum(int[] nums, int target) { Dictionary<int, int> numIndices = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { int complement = target - nums[i]; if (numIndices.ContainsKey(complement)) { returnnewint[]{numIndices[complement], i}; } numIndices[nums[i]] = i; } returnnewint[]{}; } }
classSolution { functwoSum(_nums: [Int], _target: Int) -> [Int] { var numIndices = [Int: Int]() for (index, num) in nums.enumerated() { let complement = target - num iflet complementIndex = numIndices[complement] { return [complementIndex, index] } numIndices[num] = index } return [] } }
结果
执行用时 : 36 ms, 击败 93.84% 使用 Swift 的用户
内存消耗 : 14.39 MB, 击败 7.81% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { fun twoSum(nums: IntArray, target: Int): IntArray { valnumIndices= HashMap<Int, Int>() for (i in nums.indices) { valcomplement= target - nums[i] if (numIndices.containsKey(complement)) { return intArrayOf(numIndices[complement]!!, i) } numIndices[nums[i]] = i } return intArrayOf() } }
结果
执行用时 : 204 ms, 击败 66.38% 使用 Kotlin 的用户
内存消耗 : 37.57 MB, 击败 63.17% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution{ List<int> twoSum(List<int> nums, int target) { Map<int, int> numIndices = {}; for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (numIndices.containsKey(complement)) { return [numIndices[complement]!, i]; } numIndices[nums[i]] = i; } return []; } }
结果
执行用时 : 256 ms, 击败 100.00% 使用 Dart 的用户
内存消耗 : 155.91 MB, 击败 20.20% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11
functwoSum(nums []int, target int) []int { numIndices := make(map[int]int) for i, num := range nums { complement := target - num if idx, ok := numIndices[complement]; ok { return []int{idx, i} } numIndices[num] = i } return []int{} }
结果
执行用时 : 4 ms, 击败 94.26% 使用 Go 的用户
内存消耗 : 4.02 MB, 击败 63.86% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# @param {Integer[]} nums # @param {Integer} target # @return {Integer[]} deftwo_sum(nums, target) num_indices = {} nums.each_with_index do |num, index| complement = target - num if num_indices.key?(complement) return [num_indices[complement], index] end num_indices[num] = index end return [] end
结果
执行用时 : 68 ms, 击败 75.61% 使用 Ruby 的用户
内存消耗 : 207.09 MB, 击败 41.46% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13
objectSolution{ deftwoSum(nums: Array[Int], target: Int): Array[Int] = { var numIndices = Map.empty[Int, Int] for ((num, index) <- nums.zipWithIndex) { val complement = target - num if (numIndices.contains(complement)) { returnArray(numIndices(complement), index) } numIndices += (num -> index) } Array.empty[Int] } }