方法1:滑動視窗. 時間複雜度 o(n) 、空間複雜度 o(1)
連續子陣列 ---> 【滑動視窗】
classsolution else else }}
return res > nums.length ? 0 : res;
*//**
* 寫法2
* 擴張視窗:為了找到一個可行解,找到了就不再擴張
* 收縮視窗:在長度上優化該可行解,直到條件被破壞
* 尋找下一個可行解,然後再優化到不能優化……
*/int l = 0, r = 0;
int res = nums.length + 1;
int tempsum = 0;
while (r < nums.length)
r ++;
}return res > nums.length ? 0: res;}}
leetcode 209 長度最小的陣列
題目描述 給定一個含有 n 個正整數的陣列和一個正整數 s ,找出該陣列中滿足其和 s 的長度最小的連續子陣列。如果不存在符合條件的連續子陣列,返回 0。 參考 負雪明燭 class solution object def minsubarraylen self s nums type s int ...