Skip to content

Commit 6abc9f2

Browse files
committed
算法
1 parent 409a4cd commit 6abc9f2

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)