# [LeetCode][1]Two Sum解析 -Java实现

Q:

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.

A:

public class TwoSum {
public static int[] twoSum(int[] array,int target){
for(int i=0;i<array.length;i++){
for(int j = 0;j<array.length-i;j++){
if((array[i]+array[j])==target)
return new int[]{array[i],array[j]};
}
}
return null;
}
public static void main(String[] args){
int[] array = new int[]{1,2,3,4,5,6,7,8,9};
int[] indices = TwoSum.twoSum(array, 5);
if(indices!=null){
System.out.println(indices[0]+"    "+indices[1]);
}
}
}


	public static int[] newTwoSum(int[] array,int target){
int[] targetArray = new int[target+1];
for(int i :array){
if(i>=0&&i<=target){
targetArray[i]++;
if(targetArray[target-i]>=1){
return new int[]{i,target-i};
}
}
}
return null;
}

#### leetcode 1. Two Sum -----java

2016-06-20 19:52:04

#### LeetCode题解--1. Two Sum(和为S的两个数字)

2016-01-27 22:32:25

#### Java-LeetCode-TwoSum

2016-09-06 20:35:46

#### 2Sum -- Java实现（3Sum 4Sum）待补充

2017-04-10 20:01:51

#### lintcode 两数之和(Two Sum )(Java)

2017-07-13 14:16:29

#### java算法之简单的Two Sum

2017-08-18 11:56:45

#### 【LeetCode-面试算法经典-Java实现】【001-Two Sum（求两个数的和）】

2015-07-16 06:39:39

#### leetcode中第一题twosum问题解答算法的可行性证明

2015-08-16 21:21:50

#### leetcode:two sum的三种解法

2015-04-15 20:16:13

#### LeetCode 算法题之：TwoSum

2018-03-02 18:27:18