243 字
1 分钟
Templates for leetcode problem solutions
2021-08-26
  • table of contents {.toc}

Problem description#

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Because nums[0] + nums[1] == 9, we return [0, 1].

https://leetcode-cn.com/problems/two-sum

Analysis#

Enumerate#

Go through the list and find the one matches target -x.

Hashtable#

Search in hashtable for matches of target -x then insert x itself.

Solutions#

Java#

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
}

Time complexity: O(N), where NN is the number of elements in the array. For each element x, we can find target-x O(1)O(1).

Space complexity: O(N), where NN is the number of elements in the array. Mainly the overhead of the hash table.

Golang#

func twoSum(nums []int, target int) []int {
for i, x := range nums {
for j := i + 1; j < len(nums); j++ {
if x+nums[j] == target {
return []int{i, j}
}
}
}
return nil
}

Time complexity: O(N^2), where NN is the number of elements in the array. In the worst case, any two numbers in the array must be matched once.

Space complexity: O(1).

Reference#

https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-solution/

Templates for leetcode problem solutions
https://static.next.liqimore.com/2021/template-for-leetcode-solutions
作者
Ricky Li
发布于
2021-08-26
许可协议
CC BY-NC-SA 4.0
如果评论不显示,请刷新页面重新加载. Please refresh if comments didn't show up.