当前位置:网站首页>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;
}
};
边栏推荐
- Yolov6 training: various problems encountered in training your dataset
- buuctf-pwn write-ups (7)
- Chapter 9: xshell free version installation
- 蜻蜓低代码安全工具平台开发之路
- 广州市应急管理局发布7月高温高湿化工安全提醒
- Design and implementation of car query system based on php+mysql
- < 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
- 测试框架TestNG的使用(二):testNG xml的使用
- Obsidian installs third-party plug-ins - unable to load plug-ins
- obsidian安装第三方插件——无法加载插件
猜你喜欢

Tmall product details interface (APP, H5 end)

uniapp自动化测试学习
![[apipost] tutorial](/img/f9/717908a72720f152ad49034be64b35.png)
[apipost] tutorial

< 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

为什么只会编程的程序员无法成为优秀的开发者?

LeetCode 209. 长度最小的子数组

Method of creating linked server for cross server data access

Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)

LeetCode - 搜索二维矩阵

Fabric. JS zoom canvas
随机推荐
Fabric. JS dynamically set font size
LeetCode_滑动窗口_中等_395.至少有 K 个重复字符的最长子串
记一次报错解决经历依赖重复
Xilinx Vivado set *. svh as SystemVerilog Header
MathML to latex
Actual combat sharing of shutter screen acquisition
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
为什么只会编程的程序员无法成为优秀的开发者?
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
广州市应急管理局发布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
Fabric.js 上划线、中划线(删除线)、下划线
Fundamentals of software testing
Advanced usage of C language -- function pointer: callback function; Conversion table
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
C语言中的printf函数和scanf函数
C#代码审计实战+前置知识