File tree Expand file tree Collapse file tree
main/java/com/crossoverjie/algorithm
test/java/com/crossoverjie/algorithm Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .crossoverjie .algorithm ;
2+
3+ /**
4+ * Function:
5+ *
6+ * @author crossoverJie
7+ * Date: 04/01/2018 09:53
8+ * @since JDK 1.8
9+ */
10+ public class TwoSum {
11+
12+ public int [] getTwo1 (int [] nums ,int target ){
13+ int [] result = new int [2 ] ;
14+
15+ for (int i = 0 ;i <nums .length ;i ++){
16+ int a = nums [i ] ;
17+ for (int j = nums .length -1 ;j >=0 ;j --){
18+ int b = nums [j ] ;
19+
20+ if ((a +b ) == target ){
21+ result = new int []{i ,j } ;
22+ }
23+ }
24+ }
25+ return result ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package com .crossoverjie .algorithm ;
2+
3+ import com .alibaba .fastjson .JSON ;
4+ import org .junit .Test ;
5+
6+ public class TwoSumTest {
7+ @ Test
8+ public void getTwo1 () throws Exception {
9+ TwoSum twoSum = new TwoSum () ;
10+ int [] nums ={1 ,3 ,5 ,7 };
11+ int [] two1 = twoSum .getTwo1 (nums , 12 );
12+ System .out .println (JSON .toJSONString (two1 ));
13+
14+ }
15+
16+ }
You can’t perform that action at this time.
0 commit comments