当前位置:网站首页>LeetCode 209. 长度最小的子数组
LeetCode 209. 长度最小的子数组
2022-07-02 11:26:00 【HumbleFool】
LeetCode 209. 长度最小的子数组
前缀和 + 二分
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;
}
};
滑动窗口 / 双指针
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;
}
};
边栏推荐
- Fabric. Usage of JS eraser (including recovery function)
- 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
- taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
- Yyds dry goods inventory software encryption lock function
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- Chapter 9: xshell free version installation
- Reuse and distribution
- fatal: unsafe repository is owned by someone else 的解决方法
猜你喜欢

< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv

obsidian安装第三方插件——无法加载插件

STM32 library function for GPIO initialization

STM32库函数进行GPIO初始化

##51单片机实验之简易验证码发生器

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

MQ教程 | Exchange(交换机)

Fabric.js 上划线、中划线(删除线)、下划线

什么是 eRDMA?丨科普漫画图解

buuctf-pwn write-ups (7)
随机推荐
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
跨服务器数据访问的创建链接服务器方法
mongodb的认识
mathML转latex
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
buuctf-pwn write-ups (7)
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
Convolutional neural network (Introduction)
求轮廓最大内接圆
由粒子加速器产生的反中子形成的白洞
Fabric.js 缩放画布
Threejs controller cube space basic controller + inertia control + flight control
Use of freemaker
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Yolov3 & yolov5 output result description
Bit by bit of OpenCV calling USB camera
MQ教程 | Exchange(交换机)
测试框架TestNG的使用(二):testNG xml的使用
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen