-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q57_SumInSortedArray.java
67 lines (61 loc) · 1.67 KB
/
Q57_SumInSortedArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package jianzhi_offer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author weiyuwang
* @since 2019/2/3 20:20
*/
public class Q57_SumInSortedArray {
public static void main(String[] args){
for (int[] ints : find(9)) {
System.out.println(Arrays.toString(ints));
}
}
/**
* LeetCode 167
*
* 输入一个递增排序的数组和一个数字 k,在数组中查找两个数,使得他们的和为 k。输入任意一对。
*/
public int[] twoSum(int[] numbers, int target) {
if(numbers == null || numbers.length < 2) {
return null;
}
int i = 0,j = numbers.length - 1;
while(i < j){
int sum = numbers[i] + numbers[j];
if(sum == target){
return new int[]{i + 1,j + 1};
}else if(sum < target){
i++;
}else{
j--;
}
}
return null;
}
/**
* 输入正数 target,输出所有和为 target的连续正数序列 例如:输入9=2+3+4=4+5
*/
public static List<int[]> find(int target) {
List<int[]> list = new ArrayList<>();
int small = 1;
int big = 2;
int mid = (target + 1) / 2;
while (small < mid) {
int sum = 0;
for (int i = small; i <= big; i++) {
sum += i;
}
if (sum == target) {
list.add(new int[]{small,big});
small++;
} else if (sum < target) {
big++;
} else {
small++;
}
}
return list;
}
}