Leetcode: Two Sum
来源:互联网 发布:淘宝大学讲师名单公布 编辑:程序博客网 时间:2024/06/10 10:53
Problem:
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 thatyour 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
Solution 1:
Idea: First sort the arrays. Note that we need to employ a hash map to store the corresponding indices of each number before they are sorted.
Then for each number num in the list, use binary search to check if (target - num) can be found in the numbers behind num, if so, use the hash map to find their original indices and return.
But this solution cannot pass the large data case.
Code:
public class Solution { int[] result = new int[2]; public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { map.put(numbers[i], i); } Arrays.sort(numbers); for (int i = 0; i < numbers.length - 1; i++) { int key = target - numbers[i]; int start = i + 1; int end = numbers.length - 1; while(start <= end) { int mid = (start + end) / 2; if (numbers[mid] == key) { result[0] = map.get(numbers[i]) + 1; result[1] = map.get(numbers[mid]) + 1; return result; } else if (numbers[mid] > key) { end = mid - 1; } else { start = mid + 1; } } } return result; } }
Solution2:
Also cannot pass large data case........
Code:
public class Solution { int[] result = new int[2]; public int[] twoSum(int[] numbers, int target) { HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); for (int i = 0; i < numbers.length; i++) { if (!map.containsKey(numbers[i])) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(i); map.put(numbers[i], list); } else { ArrayList<Integer> list = map.get(numbers[i]); list.add(i); map.put(numbers[i], list); } } Arrays.sort(numbers); int start = 0; int end = numbers.length - 1; while(start <= end) { if (numbers[start] + numbers[end] == target) { int index1 = map.get(numbers[start]).get(0) + 1; map.get(numbers[start]).remove(0); int index2 = map.get(numbers[end]).get(0) + 1; if (index1 > index2) { result[0] = index2; result[1] = index1; } else { result[1] = index2; result[0] = index1; } return result; } else if (numbers[start] + numbers[end] > target) { end--; } else { start++; } } return result; } }
Solution 3: O(n) solution
Code: Use hash map to store all the numbers before that number we are checking.
public class Solution { int[] result = new int[2]; public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { result[0] = map.get(target - numbers[i]) + 1; result[1] = i + 1; return result; } else { map.put(numbers[i], i); } } return result; } }
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- LeetCode | Two Sum
- WV.53-向有序数组中插入一个数
- 欢迎使用CSDN-markdown编辑器
- 11059 - Maximum Product
- 黑马程序员——ObjC基础—方法
- Qt学习入门
- Leetcode: Two Sum
- fork,你拿什么证明你的写时拷贝(COW)
- where in的sql语句按照指定ID进行排序的解决方法
- 为什么 PHPer 应当学习 Golang .
- CSS3/SVG clip-path路径剪裁遮罩属性简介
- apache 运行 discuz 论坛
- WV.54-删除有序数列中的某数
- Java中出现java.util.ConcurrentModificationException的原理探究和解决办法
- Android BitmapDrawable()的使用