Skip to content

Commit 49e95c3

Browse files
author
曾林西
committed
增加java代码,提交TwoSum.java
1 parent 74d8379 commit 49e95c3

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
*~
2+
.classpath
3+
.idea
4+
.project
5+
.settings
6+
build/
7+
bin/
8+
out/
9+
*.ipr
10+
*.iml
11+
*.iws
12+
*.iml
13+
*.ipr
14+
*.iws
15+
*.tmproj
16+
*~
17+
.DS_Store
18+
.classpath
19+
.generators
20+
.idea
21+
.project
22+
.settings
23+
**/target
24+
target
25+
build

java/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>meituan</groupId>
8+
<artifactId>leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>

java/src/main/java/TwoSum.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashMap;
2+
3+
/**
4+
* Created by zenglinxi on 15-8-1.
5+
*/
6+
public class TwoSum {
7+
public int[] twoSum(int[] nums, int target) {
8+
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
9+
int[] result = new int[2];
10+
11+
for(int i = 0; i < nums.length; i++){
12+
int one = nums[i];
13+
int two = target - one;
14+
if(hm.containsKey(two) && hm.get(two) != i + 1){
15+
result[0] = hm.get(two);
16+
result[1] = i + 1;
17+
} else {
18+
hm.put(nums[i], i + 1);
19+
}
20+
}
21+
return result;
22+
}
23+
24+
public static void main(String[] args){
25+
TwoSum ts = new TwoSum();
26+
int[] nums = {0, 4, 3, 0};
27+
int target = 0;
28+
int[] result = ts.twoSum(nums, target);
29+
for(int num : result){
30+
System.out.println(num);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)