当前位置:网站首页>LeetCode-209-长度最小的子数组
LeetCode-209-长度最小的子数组
2022-07-27 18:29:00 【z754916067】
题目

思路
- 第一反应是用二维数组存dp从i到j的总和,如果等于target的时候就算长度,只记录短的那条,但感觉数据规模上好像过不了。一维的话尝试了一下用前缀和解决。
- 但是没有想到用前缀和做二分搜索…连续子数组大部分要使用滑动窗口或者前缀和
代码
public int minSubArrayLen(int target, int[] nums) {
int left = 0,right = 0;
int ans = Integer.MAX_VALUE;
//维护一个和sum
int sum=0;
while(right<nums.length){
sum+=nums[right];
//如果加上end后>=target了
//开始左移start
while(sum>=target){
ans = Math.min(ans,right-left+1);
sum-=nums[left];
left++;
}
right++;
}
return ans == Integer.MAX_VALUE ? 0 : ans;
}
边栏推荐
- CPDA|如何拥有数据分析思维?
- To do the test, you have to go to the big factory and disclose the "hidden rules" of bat big factory recruitment internally
- Management of user organization structure
- Technology blog and tutorial
- 认识网络模型网络模型概述
- Hexagon_V65_Programmers_Reference_Manual(9)
- Download of MySQL driver jar package -- nanny tutorial
- Best practices for Oracle kingbasees migration of Jincang database (2. Overview)
- Slim: self supervised point cloud scene flow and motion estimation (iccv 2021)
- VI working mode (3 kinds) and mode switching (conversion)
猜你喜欢

【深度学习】Pytorch Tensor 张量

SLIM:自监督点云场景流与运动估计(ICCV 2021)
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片

未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
![[numpy] broadcast mechanism](/img/1f/8d61ac7b35a82067bc0b77426590eb.png)
[numpy] broadcast mechanism

hcip第五天
![[Numpy] 广播机制(Broadcast)](/img/1f/8d61ac7b35a82067bc0b77426590eb.png)
[Numpy] 广播机制(Broadcast)

2022-07-19 advanced network engineering (XX) BGP route optimization, route optimization analysis one by one

推荐一款强大的搜索工具Listary

Hexagon_V65_Programmers_Reference_Manual(6)
随机推荐
openresty lua-resty-core 使用
Riding lantern case
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
sql编码bug
征服所有程序员的3件IT装备 →
R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表(使用.符号和~符号指定函数语法purr)
Know the transmission medium, the medium of network communication
Vant component library
Management of user organization structure
How does the industrial switch enter the web management interface?
认识传输介质通信方式
技术博客及教程
Hexagon_V65_Programmers_Reference_Manual(6)
How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组加和值(sum)
Global styles and icons
JVs basic framework function list
走马灯案例
MySQL驱动jar包的下载--保姆教程
人脸识别5.1- insightface人脸检测模型训练实战笔记