首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
长度最小的连续子数组
[编程题]长度最小的连续子数组
热度指数:4167
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个数组 nums 和一个正整数 target , 找出满足和大于等于 target 的长度最短的连续子数组并返回其长度,如果不存在这种子数组则返回 0。
数据范围:数组长度满足
,数组中的元素满足
,
示例1
输入
[1,2,4,4,1,1,1],9
输出
3
示例2
输入
[1,4,4,4,1,1,1],3
输出
1
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(62)
分享
纠错
提交结果有问题?
1个回答
17篇题解
添加回答
0
C
在写面经的咸鱼很机智
// C语言竟然没人提交,那我来个代码,仿生态模式滑窗思维
int minSubarray(int* nums, int numsLen, int target ) {
// write code here
int resultA = 0, resultE= 0;
int resultLength = 0,resultLengthMin = 1000000;
int i,j,k,sum = 0;
for(i = 0; i < numsLen; ++i) {
sum += nums[i];
if(sum >= target) {
resultE = i;
resultLength = resultE - resultA + 1;
if(resultLengthMin > resultLength) {
resultLengthMin = resultLength;
}
for(j = resultA; j < i; ++j) {
sum -= nums[j];
if(sum >= target) {
resultA = j + 1;
resultLength = resultE - resultA + 1;
if(resultLengthMin > resultLength) {
resultLengthMin = resultLength;
}
}
else {
// 不能减,切记复原
sum += nums[j];
break;
}
}
}
}
return (resultLengthMin>=100000?0:resultLengthMin);
}
发表于 2023-05-17 02:58:41
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
二分
前缀和
双指针
难度:
1条回答
62收藏
5015浏览
热门推荐
通过挑战的用户
查看代码
牛客45533...
2022-09-15 22:54:43
两个柠檬
2022-09-14 21:41:26
蒋燊
2022-09-14 08:31:49
Asphyxiay
2022-09-14 01:59:07
雷欧尼桑
2022-09-13 12:13:42
相关试题
迷失的括号序列
贪心
思维
前缀和
评论
(7)
神奇的数字
排序
双指针
评论
(46)
航海
排序
思维
二分
评论
(1)
在大语言模型中,什么是"Gated...
大模型开发
评论
(1)
关于大模型“上下文窗口”的理解,以...
大模型概念
评论
(1)
长度最小的连续子数组
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int minSubarray (int[] nums, int target) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型 */ int minSubarray(vector
& nums, int target) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def minSubarray(self , nums , target ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int minSubarray (List
nums, int target) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ function minSubarray( nums , target ) { // write code here } module.exports = { minSubarray : minSubarray };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def minSubarray(self , nums: List[int], target: int) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ func minSubarray( nums []int , target int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param target int整型 * @return int整型 */ int minSubarray(int* nums, int numsLen, int target ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution def minSubarray(nums, target) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ def minSubarray(nums: Array[Int],target: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ fun minSubarray(nums: IntArray,target: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int minSubarray (int[] nums, int target) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ export function minSubarray(nums: number[], target: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ func minSubarray ( _ nums: [Int], _ target: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ pub fn minSubarray(&self, nums: Vec
, target: i32) -> i32 { // write code here } }
[1,2,4,4,1,1,1],9
3
[1,4,4,4,1,1,1],3
1