File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments