LeetCode java Two Sum
2015-04-02 06:21
316 查看
The 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 that your 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
How to solve it:
First, copy the original array into a new array, then sorted the new array. Then find the two number we need in the new array, then find their corresponding index in the original array.
Time complexity is O(nlogn) +O(n) +O(n) = O(nlogn)
Space complexity is O(n).
Code:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int N = numbers.length;
int[] sorted = new int
;//build a new array
System.arraycopy(numbers, 0, sorted, 0, N);//copy the old array to the new one
Arrays.sort(sorted);//sort it
//find the two numbers using the sorted arrays
int first = 0;//two pointer. One from the beginning of the array,one from the end of the array
int second = sorted.length - 1;
while(first < second){
if(sorted[first] + sorted[second] < target){
first++;
continue;
}
else if(sorted[first] + sorted[second] > target){
second--;
continue;
}
else break;
}
int number1 = sorted[first];
int number2 = sorted[second];
There is another way to solve this by using hashmap, which has O(n) complexity. In fact there is no difference than brute force, is just that the searching speed of hashmap is O(1).
public int[] twoSum(int[] numbers, int target) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int n = numbers.length;
int[] result = new int[2];
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;
break;
}
else
{
map.put(numbers[i], i);
}
}
return result;
}
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 that your 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
How to solve it:
First, copy the original array into a new array, then sorted the new array. Then find the two number we need in the new array, then find their corresponding index in the original array.
Time complexity is O(nlogn) +O(n) +O(n) = O(nlogn)
Space complexity is O(n).
Code:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int N = numbers.length;
int[] sorted = new int
;//build a new array
System.arraycopy(numbers, 0, sorted, 0, N);//copy the old array to the new one
Arrays.sort(sorted);//sort it
//find the two numbers using the sorted arrays
int first = 0;//two pointer. One from the beginning of the array,one from the end of the array
int second = sorted.length - 1;
while(first < second){
if(sorted[first] + sorted[second] < target){
first++;
continue;
}
else if(sorted[first] + sorted[second] > target){
second--;
continue;
}
else break;
}
int number1 = sorted[first];
int number2 = sorted[second];
//Find the two indexes in the original array int index1 = -1, index2 = -1; for(int i = 0; i < N; i++){ if((numbers[i] == number1) || (numbers[i] == number2)){ if(index1 == -1) index1 = i + 1; else index2 = i + 1; } } int [] result = new int[]{index1, index2}; Arrays.sort(result); return result; } }
There is another way to solve this by using hashmap, which has O(n) complexity. In fact there is no difference than brute force, is just that the searching speed of hashmap is O(1).
public int[] twoSum(int[] numbers, int target) {
// Start typing your Java solution below
// DO NOT write main() function
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int n = numbers.length;
int[] result = new int[2];
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;
break;
}
else
{
map.put(numbers[i], i);
}
}
return result;
}
相关文章推荐
- Two Sum leetcode java
- LeetCode1 TwoSum Java
- Leetcode 第一题 Two Sum java代码
- [leetcode-371]Sum of Two Integers(java)
- leetcode-Java-1. Two Sum.java
- 【Leetcode】two sum JAVA
- LeetCode 1 — Two Sum(C++ Java Python)
- LeetCode – Two Sum (Java) —题解
- [Leetcode] Two Sum (Java)
- Two Sum Leetcode Python Java
- Leetcode-1 Two Sum(Java) -by zzy
- [leetcode-1]Two Sum(java)
- leetcode: two sum, hash search solution, java. O(n)
- LeetCode 1 - Two Sum ( JAVA )
- LeetCode: Two Sum (Java)
- LeetCode 1: Two Sum (JAVA)
- LeetCode 1 Two sum Java
- 【LeetCode-面试算法经典-Java实现】【001-Two Sum(求两个数的和)】
- leetcode 1. Two Sum -----java
- [LeetCode][1]Two Sum解析 -Java实现