-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_57_FindNumbersWithSum.java
71 lines (64 loc) · 1.75 KB
/
_57_FindNumbersWithSum.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
68
69
70
71
package swordtooffer;
import java.util.ArrayList;
import java.util.List;
/**
* @Description 和为s的数字
*/
public class _57_FindNumbersWithSum {
// 递增排序的数组和target,查找两个数字和为target
public static int[] findNumbersWithSum(int[] arr, int target) {
int[] res = new int[2];
if (arr == null || arr.length == 0) {
return res;
}
int l = 0;
int r = arr.length - 1;
while (l <= r) {
int tmp = arr[l] + arr[r];
if (tmp == target) {
res[0] = arr[l];
res[1] = arr[r];
return res;
} else if (tmp < target) {
l++;
} else {
r--;
}
}
return res;
}
// 寻找所有和为s的连续正数序列
public static List<List<Integer>> findContinuousSequence(int num) {
if (num < 3) {
return null;
}
int l = 1;
int r = 2;
int curSum = 3;
int end = (1 + num) / 2;
List<List<Integer>> res = new ArrayList<>();
while (l < end) {
if (curSum == num) {
List<Integer> tmp = new ArrayList<>();
for (int i = l; i <= r; i++) {
tmp.add(i);
}
res.add(tmp);
r++;
curSum += r;
} else if (curSum > num) {
curSum -= l;
l++;
} else {
r++;
curSum += r;
}
}
return res;
}
public static void main(String[] args) {
List<List<Integer>> res;
res = findContinuousSequence(15);
System.out.println(res);
}
}