[LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
给一个没排序的包含整数的数组,找出使得两个数的和是给定值的indices。假设每个输入只有一个答案, 同一元素不会用到两次。
解法:
1. 暴力解法,两个for循环遍历,两个数相加和目标数比较。Time: O(n^2)
2. 先对数组快速排序,然后用两个指针分别指向头和尾,每次比较头尾两个数的和,如果比target小,头标记右移,如果大,尾标记左移。需要注意记录快速排序前后数字的位置变化。Time: O(n)
3. 先遍历一遍数组,建立数字和index的HashMap,然后再遍历一遍,开始查找target - num[i]是否在map中,如果在,找到并返回index。Time: O(n) Space: O(n)
Java:解法1
public static int[] twoSum(int[] numbers, int target) { int[] ret = new int[2]; for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] + numbers[j] == target) { ret[0] = i + 1; ret[1] = j + 1; } } } return ret; }
Java:解法2
class Pair implements Comparable<Pair>{ public int number; public int idx; public Pair(int number, int idx){ this.number = number; this.idx = idx; } public int compareTo(Pair other){ return this.number - other.number; } } public class Solution { public int[] twoSum(int[] numbers, int target) { int n = numbers.length; Pair[] pairs = new Pair[n]; for(int i = 0; i < n; ++i){ pairs[i] = new Pair(numbers[i], i + 1); } Arrays.sort(pairs); int [] result = new int[2]; int begin = 0; int end = n - 1; while(begin < end){ if(pairs[begin].number + pairs[end].number < target){ begin++; } else if (pairs[begin].number + pairs[end].number > target){ end--; } else{ if(pairs[begin].idx > pairs[end].idx){ result[0] = pairs[end].idx; result[1] = pairs[begin].idx; }else{ result[0] = pairs[begin].idx; result[1] = pairs[end].idx; } break; } } return result; } }
Java: 解法3,Two loops
public class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); int[] res = new int[2]; for (int i = 0; i < nums.length; ++i) { m.put(nums[i], i); } for (int i = 0; i < nums.length; ++i) { int t = target - nums[i]; if (m.containsKey(t) && m.get(t) != i) { res[0] = i; res[1] = m.get(t); break; } } return res; } }
Java: 解法3,one loop
public class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); int[] res = new int[2]; for (int i = 0; i < nums.length; ++i) { if (m.containsKey(target - nums[i])) { res[0] = i; res[1] = m.get(target - nums[i]); break; } m.put(nums[i], i); } return res; } }
Python:
class Solution(object): def twoSum(self, nums, target): hash_map = {} for i, v in enumerate(nums): hash_map[v] = i for index1, value in enumerate(nums): if target - value in hash_map: index2 = hash_map[target - value] if index1 != index2: return [index1, index2]
Python:
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ lookup = {} for i, num in enumerate(nums): if target - num in lookup: return [lookup[target - num], i] lookup[num] = i
Python: wo
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ lookup = {} for i in range(len(nums)): if target - nums[i] in lookup: return [lookup[target - nums[i]], i] lookup[nums[i]] = i
Python:
class Solution(object): def twoSum(self, nums, target): if len(nums) <= 1: return False buff_dict = {} for i in range(len(nums)): if nums[i] in buff_dict: return [buff_dict[nums[i]], i] else: buff_dict[target - nums[i]] = i
C++:
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> lookup; for (int i = 0; i < nums.size(); ++i) { if (lookup.count(target - nums[i])) { return {lookup[target - nums[i]], i}; } lookup[nums[i]] = i; } return {}; } };
相似题目:
[LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
[LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
[LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
All LeetCode Questions List 题目汇总