【作业3】剑指 Offer 57 – II. 和为s的连续正数序列
对应课程链接:拿捏算法面试:8. LeetCode209. 长度最小的子数组
这道题也是用滑动窗口哦,和我们课程中讲解的那一道很像,可以当作练习题来做哦,作业要求:
1、给出 剑指 Offer 57 – II. 和为s的连续正数序列]
运行通过的代码。
PS:如果你不知道怎么打卡,可以看这里:打卡细则
对应课程链接:拿捏算法面试:8. LeetCode209. 长度最小的子数组
这道题也是用滑动窗口哦,和我们课程中讲解的那一道很像,可以当作练习题来做哦,作业要求:
1、给出 剑指 Offer 57 – II. 和为s的连续正数序列]
运行通过的代码。
PS:如果你不知道怎么打卡,可以看这里:打卡细则
共以下 32 个回答
//代码class Solution {
public int[][] fileCombination(int target) {
if(target <span class="text-highlighted-inline" style="background-color: #fffd38;"> 0){
return null;
}
int l = 1, r = 1;
List<int[]> res = new ArrayList<>();
int s = 0;
while(r < target){
s += r;
while(s >= target){
if(s </span> target){
int[] arr = new int[r - l + 1];
for(int j = l; j <= r; j++){
arr[j - l] = j;
}
res.add(arr);
}
s -= l;
l++;
}
r++;
}
return res.toArray(new int[res.size()][]);
}
}
“`python代码
class Solution:
def fileCombination(self, target: int) -> List[List[int]]:
if target<=0: return -1
“`
”’class Solution {
public int[][] findContinuousSequence(int target) {
int l = 1, r = 2; // 初始化左右边界,构成初始序列 [1, 2]
int sum = 3; // 初始化当前序列的和为 3
List<int[]> list = new ArrayList<>(); // 用于存储符合条件的序列
}
”’java
打卡
滑动窗口
这道题的 r < target 这个条件还有继续优化
更新后的代码:
C++版本:
打卡
滑动窗口
滑动窗口
class Solution {
public int[][] findContinuousSequence(int target) {
}
滑动窗口,时间复杂度O(N),空间复杂度O(1)。
时间复杂度O(n) 空间复杂度O(1)
滑动窗口,时间复杂度O(n)
“`gofunc findContinuousSequence(target int) [][]int {
l, r := 1,1
sum := 0
ans := make([][]int, 0)
}
“`
时间复杂度:$O(n)$
时间复杂度O(n)
作业三打卡
双指针