forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
52 lines (42 loc) · 1.2 KB
/
Solution.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
/**
* @author mcrwayfun
* @version v1.0
* @date Created in 2019/02/03
* @description
*/
public class Solution {
public List<List<Integer>> findContinuousSequence(int sum) {
List<List<Integer>> reList = new ArrayList<>();
if (sum < 3) {
return reList;
}
int left = 1;
int right = 2;
int mid = (sum + 1) / 2;
int curSum = left + right;
// left小于sum一半即可(1/2n)
while (left < mid) {
// 等与sum则加入列表中(2~1/2n)
if (curSum == sum) {
reList.add(getListFromleftToright(left, right));
// right增加并重新寻找序列
right++;
curSum += right;
} else if (curSum > sum) {
curSum -= left;
left++;
} else {
right++;
curSum += right;
}
}
return reList;
}
private List<Integer> getListFromleftToright(int left, int right) {
List<Integer> tempList = new ArrayList<>();
for (int i = left; i <= right; i++) {
tempList.add(i);
}
return tempList;
}
}