当前位置:网站首页>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;
}
};
边栏推荐
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- 3、函数指针和指针函数
- Li Chuang EDA learning notes 15: draw border or import border (DXF file)
- 4、数组指针和指针数组
- Fabric. JS free drawing ellipse
- Fabric.js 缩放画布
- MQ tutorial | exchange (switch)
- LeetCode 2310. 个位数字为 K 的整数之和
- Fabric. JS zoom canvas
- Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
猜你喜欢
LeetCode 2310. 个位数字为 K 的整数之和
Ad20 cannot select the solution of component packaging in PCB editor
Pychart connects to the remote server
LeetCode 2320. 统计放置房子的方式数
mathjax 入门(web显示数学公式,矢量的)
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
MQ教程 | Exchange(交换机)
PHP linked list creation and traversal
fatal: unsafe repository is owned by someone else 的解决方法
随机推荐
Large top heap, small top heap and heap sequencing
Implement a server with multi process concurrency
JMeter script parameterization
Quick analysis: easy to share the Internet
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
info [email protected]: The platform “win32“ is incompatible with this module.
Fabric.js 上划线、中划线(删除线)、下划线
socket(套接字)与socket地址
Fabric. Keep the original level when JS element is selected
Factal: Unsafe repository is owned by someone else Solution
华为面试题: 没有回文串
LeetCode 2310. 个位数字为 K 的整数之和
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
mathML转latex
Xilinx Vivado set *. svh as SystemVerilog Header
C语言中的算术运算及相关练习题
Fabric.js 手动加粗文本iText
Design and implementation of car query system based on php+mysql
Fabric.js 自由绘制圆形