Leetcode
来源:互联网 发布:音乐降调软件中文版 编辑:程序博客网 时间:2024/06/15 02:00
题目:
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.
解析:
/思路://设置两个变量l,r,求这个子数组里的值和sum//1.sum < s r++//2.sum > s l++//直至r = nums.length -1class Solution { public int minSubArrayLen(int s, int[] nums) { int l = 0 ; int r = -1 ; int sum = 0; int res = nums.length + 1; while ( l < nums.length){ if( r + 1 < nums.length && sum < s){ r++; sum = sum + nums[r]; } else{ sum = sum - nums[l]; l++; } if( sum >= s ){ res = Math.min( res, r + l -1); } } if(res == nums.length + 1) return 0; return res; }}
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- JVectorMap插件使用
- HTML
- python模块学习(threading模块中的Condition类)
- 卷积神经网络(CNN)学习笔记
- Spring依赖注入方式
- Leetcode
- CocoaPods报错
- 苹果6s刷机教程,苹果6s Plus刷机教程
- 杭电ACM OJ 1008 Elevator 其实就是简单的加减法 有点水
- jenkins的安装配置,插件配置,安装插件,配置maven,jdk路径,配置安装插件,项目配置,自动构建配置
- gdb工具常用命令
- [2017纪中11-9]玩游戏 最短路数+LCA
- Onvif开发之客户端搜索篇
- session知识小解