public int[][] findContinuousSequence(int target) {
if (target < 3) return new int[0][];
List<int[]> res = new ArrayList<>();
for (int lo = 1, hi = 2, sum = 3; hi <= target/2 + 1;) {
if (sum < target) {
hi++;
sum += hi;
}
else if (sum > target) {
sum -= lo;
lo++;
}
else {
int[] tmp = new int[hi-lo+1];
for (int i = lo, j = 0; i <= hi; i++, j++) {
tmp[j] = i;
}
res.add(tmp);
hi++;
sum += hi;
}
}
return res.toArray(new int[res.size()][]);
}
剑指 Offer 57 - II. 和为s的连续正数序列
最新推荐文章于 2020-10-01 09:40:48 发布