“`java
暴力
class Solution {
public int[][] fileCombination(int target) {
List list = new ArrayList<>();
int limit = (target – 1)/2;
for (int i = 1; i <= limit; i ++) {
int sum = i;
int j = i+1;
while (sum < target) {
sum += j++;
}
if (sum == target) {
int[] arr = new int[j - i];
for (int k = i; k < j; k++) {
arr[k - i] = k;
}
list.add(arr);
}
}
int[][] res = new int[list.size()][];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}
int left = 1;
int right = 2;
int sum = 3; // 初始左右两个数的和
while (left < right && left <= target / 2) {
if (sum == target) {
std::vector<int> sequence;
for (int i = left; i <= right; i++) {
sequence.push_back(i);
}
result.push_back(sequence);
sum -= left; // 左边界右移,更新和
left++;
} else if (sum < target) {
right++;
sum += right; // 右边界右移,更新和
} else {
sum -= left; // 左边界右移,更新和
left++;
}
}
return result;
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
i, j = 1, 1
curSum = 1
res = []
while i <= target // 2:
if curSum < target:
j += 1
curSum += j
elif curSum > target:
curSum -= i
i += 1
else:
tmp = list(range(i, j + 1))
curSum -= i
i += 1
j += 1
curSum += j
res.append(tmp)
return res
int **findContinuousSequence(int target, int *returnSize, int **returnColumnSizes)
{
int **res;
int *col;
int x = 1, y = 2;
int limit = target / 2;
int sum;
int m = 0;
res = (int **)malloc(sizeof(int *) * target);
col = (int *)malloc(sizeof(int) * target);
sum = x + y;
while (x <= limit)
{
if (sum < target)
{
y++;
sum += y;
}
else if (sum > target)
{
sum -= x;
x++;
}
else
{
col[m] = y - x + 1;
res[m] = (int *)malloc(sizeof(int) * col[m]);
for (int i = 0; i < col[m]; i++)
res[m][i] = i + x;
m++;
sum -= x;
x++;
}
}
*returnSize = m;
*returnColumnSizes = col;
return res;
}
class Solution {
public int[][] findContinuousSequence(int target) {
int[] sum = new int[((target + 1) >> 1) + 1];
for (int i = 1; i < sum.length; i++) {
sum[i] = sum[i - 1] + i;
}
List<int[]> ans = new ArrayList<>();
int left = 0, right = 2;
while (right < sum.length) {
int temp = sum[right] - sum[left];
if (temp == target) {
int[] s = new int[right - left];
for (int i = 0; i < right - left; i++) {
s[i] = i + left + 1;
}
ans.add(s);
right++;
} else if (temp < target) {
right++;
} else {
left++;
}
}
return ans.toArray(new int[ans.size()][]);
}
}
双指针+数学优化
时间:O(target)
空间:O(1)
class Solution {
public int[][] findContinuousSequence(int target) {
int l = 1, r = 2;
List<int[]> ansList = new ArrayList<>();
while (r <= (target + 1) / 2) {
int sum = (l + r) * (r - l + 1) / 2;
if (sum == target) {
int[] sub = new int[r - l + 1];
for (int i = 0; i < sub.length; i++) {
sub[i] = l + i;
}
ansList.add(sub);
r++;
} else if (sum < target) {
r++;
} else {
l++;
}
}
return ansList.toArray(new int[ansList.size()][]);
}
}
public int[][] findContinuousSequence(int target) {
List<int[]> list = new ArrayList<>();
int i = 1;
int j = 1;
int sum = 1;
while(i <= target / 2) {
if(sum > target) {
sum = sum - i;
i++;
} else if(sum < target) {
j++;
sum += j;
} else {
int[] temp = new int[j-i+1];
int index = 0;
for(int k = i; k <= j; k++) {
temp[index++] = k;
}
list.add(temp);
sum = sum - i;
i++;
j++;
sum = sum +j;
}
}
return list.toArray(new int[list.size()][]);
}
public int[][] findContinuousSequence(int target) {
int n = target/2+1;
int l = 1, r = 2;
int cur = 3;
List<int[]> res = new LinkedList<>();
while(r <= n){
if(cur == target){
int[] combine = new int[r-l+1];
for(int i = l; i <= r; i++){
combine[i-l] = i;
}
res.add(combine);
}
if(cur >= target){
cur -= l++;
}else{
r++;
cur += r;
}
}
return res.toArray(new int[res.size()][]);
}
public int[][] findContinuousSequence(int target) {
//初始窗口
int left = 1;
int right = 1;
//初始当前sum
int sum = 1;
//记录结果
List<List<Integer>> res = new ArrayList<>();
while (left <= target / 2){
if(sum < target){
right++;
sum = sum + right;
} else if (sum > target) {
sum = sum - left;
left++;
}else{
//记录
List<Integer> cur = new ArrayList<>();
for(int i = left;i <= right;i++){
cur.add(i);
}
res.add(cur);
sum = sum - left;
left++;
right++;
sum = sum + right;
}
}
//将列表转为二维数组
int [][] dic= new int[res.size()][];
for(int i = 0; i < res.size(); i++){
dic[i] = new int[res.get(i).size()];
for(int j = 0; j < dic[i].length; j++){
dic[i][j] = res.get(i).get(j);
}
}
return dic;
}
// 滑动窗口
class Solution {
public int[][] findContinuousSequence(int target) {
int left = 1, right = 2, sum = 3;
List array = new ArrayList<>();
while (left < right) {
if (sum == target) { //记录连续整数序列,并向右移动左边界 left++
int[] answers = new int[right - left + 1];
for (int index = left; index <= right; index++) {
answers[index - left] = index;
}
array.add(answers);
}
if (sum >= target) { // 向右移动左边界 left++,更新 sum
sum -= left;
left++;
} else { // 向右移动右边界 right++,更新 sum
right++;
sum += right;
}
}
return array.toArray(new int[0][]);
}
}
class Solution {
public int[][] findContinuousSequence(int target) {
List<int[]> list = new ArrayList<>();
int i = 1, j = 1, sum = 1;
while(i <= target / 2){
if(sum < target) {
j++;
sum = sum + j;
} else if (sum > target){
sum = sum - i;
i++;
} else {
int[] nums = new int[j - i + 1];
int index = 0;
for(int k = i; k <= j; k++){
nums[index++] = k;
}
list.add(nums);
sum = sum - i;
i++;
}
}
return list.toArray(new int[list.size()][]);
}
}
class solution{
public:
vector<vector<int>> findContinuousSequence(int target){
vector<vector<int>> res;
int i=1, j=1;
int sum=1;
while (i<=target/2){
if(sum<target){
j++;
sum +=j;
}else if{sum>target}{
sum -= i;
i++;
}else{
vector<int> temp(j-i+1);
int index = 0;
for (int k =i; k<=j; k++){
temp[index++] =k;
}
sum -= i;
i++;
j++;
sum+=j;
res.push_back(temp);
}
}
return res;
}
};
共以下 23 个回答
“`java list = new ArrayList<>();
暴力
class Solution {
public int[][] fileCombination(int target) {
List
int limit = (target – 1)/2;
for (int i = 1; i <= limit; i ++) { int sum = i; int j = i+1; while (sum < target) { sum += j++; } if (sum == target) { int[] arr = new int[j - i]; for (int k = i; k < j; k++) { arr[k - i] = k; } list.add(arr); } } int[][] res = new int[list.size()][]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }
class Solution {
public int[][] findContinuousSequence(int target) {
List<int[]> list = new ArrayList<>();
int i = 1,j=1;
int sum =1;
while(i<=target/2){
if(sum<target){
j++;
sum=sum+j;
}else if(sum>target){
sum=sum-i;
i++;
}else{
int t = 0;
int[]tmp = new int[j-i+1];
for(int k=i;k<=j;k++){
tmp[t]= k;
t++;
}
list.add(tmp);
sum= sum-i;
i++;
j++;
sum=sum+j;
}
}
return list.toArray(new int[list.size()][]);
}
}
双指针思想
“`java res=new ArrayList<>();
class Solution {
public int[][] findContinuousSequence(int target) {
int i=1;
int j=2;
int s=3;
List
while(i
} if(s>=target){ s-=i; i++; }else if(s<target){ j++; s+=j; } } return res.toArray(new int[0][]); }}
时间复杂度O(n), 空间复杂度为O(1)
双指针,时间复杂度O(n) 空间复杂度O(1)
“`C++> findContinuousSequence(int target) {> result;
vector
vector
}
“`
滑动窗口
记得每种情况都要更新指针
时间复杂度为O(target),空间复杂度为O(target)。
【思路】利用滑动窗口完成
【复杂度】时间O(n),空间O(1)
双指针+前缀和
时间:O(n)
空间:O(n)
双指针+数学优化
时间:O(target)
空间:O(1)
时间复杂度O(n) O(1) 思路:滑动窗口
思路:滑动窗口思想,因为是连续整数序列,所以可以使用两个端口即可维护一个滑动窗口
时间复杂度O(n) 空间复杂度O(1)
int[][] 的动态生成,第一次遇到
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(n)
MY帅弟
时间复杂度:O(n)
额外空间复杂度:O(1)