当前位置:网站首页>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;
}
};
边栏推荐
- Obsidian installs third-party plug-ins - unable to load plug-ins
- NLA natural language analysis makes data analysis more intelligent
- 字符串匹配问题
- PHP linked list creation and traversal
- Yyds dry goods inventory software encryption lock function
- taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
- Basic knowledge of QT original code
- < 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
猜你喜欢
< 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
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
Fabric. JS zoom canvas
Obsidian installs third-party plug-ins - unable to load plug-ins
Method of creating linked server for cross server data access
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
Teamtalk source code analysis win client
STM32-DAC实验&高频DAC输出测试
Fabric. JS free draw circle
随机推荐
Daily learning 2
MathML to latex
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
4. Array pointer and pointer array
Fabric. JS upper dash, middle dash (strikethrough), underline
YoloV6训练:训练自己数据集遇到的各种问题
Tip: SQL Server blocked the state 'openrowset/opendatasource' of component 'ad hoc distributed queries'
Actual combat sharing of shutter screen acquisition
1、编辑利器vim
fatal: unsafe repository is owned by someone else 的解决方法
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
php链表创建和遍历
STM32 library function for GPIO initialization
Tmall product details interface (APP, H5 end)
Socket and socket address
实现一个多进程并发的服务器
STM32 standard firmware library function name (I)
docker mysql
Fabric.js 上划线、中划线(删除线)、下划线
Stm32-dac Experiment & high frequency DAC output test