当前位置:网站首页>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. JS manual bold text iText
- tmall. product. schema. Get (product information acquisition schema acquisition), Taobao store upload commodity API interface, Taobao commodity publishing interface, Taobao commodity upload API interf
- Design and implementation of car query system based on php+mysql
- 1. Editing weapon VIM
- JMeter script parameterization
- 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
- 数据库连接池和数据源
- Use of freemaker
- NLA natural language analysis realizes zero threshold of data analysis
- kityformula-editor 配置字号和间距
猜你喜欢

Yolov6 training: various problems encountered in training your dataset

Fabric. JS free draw circle

ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署

Obsidian installs third-party plug-ins - unable to load plug-ins

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

Wechat applet uses towxml to display formula

fatal: unsafe repository is owned by someone else 的解决方法

The use of TestNG, the testing framework (II): the use of TestNG XML

Daily learning 3

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
随机推荐
Chapter 9: xshell free version installation
Daily learning 2
JMeter script parameterization
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Yyds dry goods inventory software encryption lock function
info [email protected]: The platform “win32“ is incompatible with this module.
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
mathjax 入门(web显示数学公式,矢量的)
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
freemarker的使用
Teamtalk source code analysis win client
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
4. Array pointer and pointer array
Fabric. JS zoom canvas
Fabric.js 自由绘制圆形
Large top heap, small top heap and heap sequencing
PHP linked list creation and traversal
Wechat applet uses towxml to display formula
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)