国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

209. Minimum Size Subarray Sum

2019-11-08 02:17:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the PRoblem constraint.

解題思路:移動(dòng)兩個(gè)指針,指針之間的窗口保持比s大,通過(guò)移動(dòng)兩個(gè)指針尋找最小的size。

public class Solution { public int minSubArrayLen(int s, int[] nums) { int res = Integer.MAX_VALUE; int i = 0, j = 0, sum = 0; while (j < nums.length) { sum += nums[j++]; while (sum >= s) { res = Math.min(res, j-i); sum -= nums[i++]; } } return res == Integer.MAX_VALUE ? 0 : res; }}class Solution {public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX; int i = 0, j = 0, sum = 0; while (j < nums.size()) { sum += nums[j++]; while (sum >= s) { res = min(res, j-i); sum -= nums[i++]; } } return res == INT_MAX ? 0 : res; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江安县| 准格尔旗| 孝义市| 福清市| 炎陵县| 科技| 新田县| 乌兰浩特市| 凤冈县| 福泉市| 普定县| 淄博市| 伊宁县| 水富县| 宁安市| 长宁区| 巧家县| 准格尔旗| 湄潭县| 英吉沙县| 眉山市| 平塘县| 若羌县| 阿瓦提县| 鄂托克旗| 黎城县| 喀什市| 冀州市| 新兴县| 陇西县| 磴口县| 洛隆县| 革吉县| 佛坪县| 施秉县| 徐汇区| 教育| 青铜峡市| 资兴市| 雷山县| 临沂市|