Two Sum
2018-02-08 15:41
99 查看
Description
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].
Discuss
题意:给定一个整形数组和一个特定的数,数组中有两个数的和等于这个特定的数。
返回这两个的数的下标
思考:
因为涉及到数组中的两个数相加,那么可以使用双层循环的方法来进行测试。
优化:
Solution
MySolution:public int[] twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[j] == target - nums[i]) { return new int[] { i, j }; } } } throw new IllegalArgumentException("No two sum solution"); }
时间复杂度:O(n²)
空间复杂度:O(1)
Better Solution:
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { map.put(nums[i], i); } for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement) && map.get(complement) != i) { return new int[] { i, map.get(complement) }; } } throw new IllegalArgumentException("No two sum solution"); }
时间复杂度: O(n)
空间复杂度: O(n)
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[] { map.get(complement), i }; } map.put(nums[i], i); } throw new IllegalArgumentException("No two sum solution"); }
相关文章推荐
- Two Sum
- LeetCode 1 Two Sum
- 【leetcode】【1】Two Sum
- 13.leetcode题目1: Two Sum
- 【一天一道LeetCode】 #1 Two Sum
- LeetCode(java)1. Two Sum
- Leetcode--Two Sum
- LeetCode|Two Sum
- Leetcode-1. Two Sum
- LeetCode 1 Two Sum 排序后快速求解
- 【leetcode】1. Two Sum
- leetcode 刷题题解(c++) 1.Two Sum (hash表,排序+二分查找)
- [LeetCode] 1. Two Sum
- Two Sum
- LeetCode: Two Sum
- Leetcode:1.Two Sum
- No.001:Two Sum
- Two Sum
- 1. Two Sum
- leetcode(E)two sum