57_和为s的连续正数序列
package 数组; /** * 题意:暗示了数据是升序的 * https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/ */ import java.util.ArrayList; import java.util.List; public class _57_和为s的连续正数序列 { public int[][] findContinuousSequence(int target) { List<int[]> vec = new ArrayList<int[]>(); int left = 1, right = 2; while(left < right) { //求和公式 int sum = (left + right) * (right - left + 1) / 2; if (sum == target) { int[] res = new int[right - left + 1]; for (int i = left; i <= right; ++i) { res[i - left] = i; } vec.add(res); left++; //找到之后,左边指针往前挪动,(微调) } else if (sum < target) { right++; } else { left++; } } return vec.toArray(new int[vec.size()][]); } }