57_和为s的连续正数序列
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()][]); } }
本文来自博客园,作者:一乐乐,转载请注明原文链接:https://www.cnblogs.com/shan333/p/15708847.html