当前位置:网站首页>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;
}
};
边栏推荐
- 没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
- ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- STM32标准固件库函数名(一)
- 求轮廓最大内接圆
- Find the maximum inscribed circle of the contour
- MQ教程 | Exchange(交换机)
- Yolov6 training: various problems encountered in training your dataset
- Fabric. JS free drawing ellipse
- Tmall product details interface (APP, H5 end)
猜你喜欢
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
Fabric. JS upper dash, middle dash (strikethrough), underline
Uniapp automated test learning
由粒子加速器产生的反中子形成的白洞
MQ tutorial | exchange (switch)
Daily learning 3
没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法
Fabric. Usage of JS eraser (including recovery function)
Stm32-dac Experiment & high frequency DAC output test
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
随机推荐
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
3. Function pointers and pointer functions
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
跨服务器数据访问的创建链接服务器方法
NLA natural language analysis makes data analysis more intelligent
Fabric. JS free draw circle
Tmall product details interface (APP, H5 end)
Xilinx Vivado set *.svh as SystemVerilog Header
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
Use of freemaker
mathjax 入门(web显示数学公式,矢量的)
Fabric. JS upper dash, middle dash (strikethrough), underline
3、函数指针和指针函数
fatal: unsafe repository is owned by someone else 的解决方法
Fabric.js 上划线、中划线(删除线)、下划线
mongodb的认识
Why can't browsers read JSX?
Yyds dry goods inventory software encryption lock function