当前位置:网站首页>LeetCode 209. Minimum length subarray
LeetCode 209. Minimum length subarray
2022-07-02 14:53:00 【HumbleFool】
LeetCode 209. Subarray with the smallest length 
The prefix and + Two points
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int n = nums.size();
int s[n + 1] = {
0};
for(int i = 1; i <= n; i ++) s[i] = s[i - 1] + nums[i - 1];
int res = 1e9;
for(int i = 1; i <= n; i ++)
{
int t = s[i] - target;
int l = 0, r = n;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(s[mid] <= t) l = mid;
else r = mid - 1;
}
if(s[r] <= t) res = min(res, i - r);
}
return res == 1e9 ? 0 : res;
}
};
The sliding window / Double pointer
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sum = 0, res = 1e9;
for(int l = 0, r = 0; r < nums.size(); r ++)
{
sum += nums[r];
while(sum >= target)
{
res = min(res, r - l + 1);
sum -= nums[l ++];
}
}
return res == 1e9 ? 0 : res;
}
};
边栏推荐
- String matching problem
- C语言中的printf函数和scanf函数
- info [email protected]: The platform “win32“ is incompatible with this module.
- Delete element (with transition animation)
- Fabric.js 动态设置字号大小
- taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
- Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
- Fatal: unsafe repository is owned by someone else
- STM32 standard firmware library function name (I)
- btrace-(字节码)动态跟踪工具
猜你喜欢

Xilinx Vivado set *. svh as SystemVerilog Header

LeetCode - 搜索二维矩阵

天猫商品详情接口(APP,H5端)

Uniapp automated test learning

Reuse and distribution

Factal: Unsafe repository is owned by someone else Solution

由粒子加速器产生的反中子形成的白洞

C code audit practice + pre knowledge
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

实现一个多进程并发的服务器
随机推荐
geoserver离线地图服务搭建和图层发布
C code audit practice + pre knowledge
PHP linked list creation and traversal
LeetCode 2320. 统计放置房子的方式数
NLA natural language analysis realizes zero threshold of data analysis
uniapp自动化测试学习
Makefile separates file names and suffixes
kityformula-editor 配置字号和间距
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
Fabric. Keep the original level when JS element is selected
蜻蜓低代码安全工具平台开发之路
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
为什么只会编程的程序员无法成为优秀的开发者?
mathML转latex
Development and design of animation surrounding mall sales website based on php+mysql
socket(套接字)与socket地址
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
华为面试题: 没有回文串
关于网页中的文本选择以及统计选中文本长度