Leetcode 523. Continuous Subarray Sum

思科路由器、交换机组网实验报告 通过思科模拟器搭建路由交换网络 阅读详情

Leetcode 523. Continuous Subarray Sum

source url

题目描述

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

Input: [23, 2, 4, 6, 7], k=6
Output: True

Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.

Example 2:

Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.

Note:
The length of the array won’t exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

输入:数组及整数k
输出:该数组的子数组(至少包含两个元素)是否能被k整除

思路

贪心:如题目所示,由于题目要求为子数组,即各元素要求连续。考虑通过计算偏序和的方式,以psum[i]表示到第i个元素的和,某两个元素之间的和可以通过psum[j]-psum[i]来表示。
若存在一个(i,j)使得(psum[j]-psum[i])%k==0 && j-i>=2则存在,否则不存在

复杂度 O(n2)

代码

class Solution {
public:

    bool checkSubarraySum(vector<int>& nums, int k) {
        if(nums.size()<2) return false;
        vector<int> psum(nums.size()+1,0);
        partial_sum( nums.begin(), nums.end(), psum.begin()+1);

        int cnt = 0;
        for(int i = 0;i<=nums.size()-2;i++){
            cnt = 0;
            for(int j = i+2;j<=nums.size();j++){
                if(k!=0){
                    if((psum[j]-psum[i])%k==0)
                        return true;

                }else{
                    if((psum[j]-psum[i])==0){
                        return true;
                    }

                }
            }
        }
        return false;
    }
};
WIFI CSI动作识别数据集全解析:从数据采集到深度学习应用 本文全面解析了利用商用Wi-Fi设备的信道状态信息进行动作识别的完整流程。文章以经典WiSA数据集为例,深入剖析了CSI数据的采集原理、结构特点,并详细阐述了从原始数据预处理、特征工程到深度学习模型(如CNN-LSTM融合架构)构建与优化的关键技术,旨在为研究者提供从数据到应用的全链路实战指南。 阅读详情

相关推荐

Qualcomm平台下基于QSEE的指纹识别方案移植之四TA移植

本文只介绍TA移植的过程,不做技术分析,关于TA开发另做记录。 一、TA介绍 TA,Trusted application 可信应用,是运行于QSEE中的可执行程序。 指纹TA文件如下,形式一般是 xxx.mbn; xxx.b0 ~ xxx.b6, xxx.flist, xxx.mdt; xxx.mbn 可以理解为其它几个文件的集合,签名的时候用的就是这个文件,签完名之后,有对应的工具将它...

梦路遥 7549

【LeetCode】第523题——连续的子数组和(难度:中等)

【LeetCode】第523题——连续的子数组和(难度:中等)题目描述解题思路代码详解注意点 题目描述 给你一个整数数组 nums 和一个整数 k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组: 子数组大小 至少为 2 ,且 子数组元素总和为 k 的倍数。 如果存在,返回 true ;否则,返回 false 。 如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。 示例 1: 输入:nums = [23,2,4,6,7], k = 6 输出

Se7en_Dayz的博客 516

易灵思Ti60F100 FPGA的HyperRAM实战:从硬件连接到Native接口时序调试

本文详细介绍了易灵思Ti60F100 FPGA内置HyperRAM控制器的实战应用。文章从硬件设计要点(如400MHz+信号完整性、等长匹配)切入,重点解析了如何通过Interface Designer配置高效的Native接口,并分享了其时序详解与调试技巧,为开发者提供了一份从硬件连接到软件调试的深度指南。

weixin_29168153的博客 459

LeetCode523:连续的子数组和

LeetCode523:连续的子数组和题目说明代码及分析 题目说明 给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数。 示例1: 输入: [23,2,4,6,7], k = 6 输出: True 解释: [2,4] 是一个大小为 2 的子数组,并且和为 6。 示例2: 输入: ...

坚果 1429

[ADS]单端电感和差分电感的感值和Q值计算公式

单端电感和差分电感的感值和Q值计算公式

乾巽的博客 1万+

逐行逐句进一步了解SurroundOcc(一)

通过分析中的模型构建过程,理解了从配置文件到模型实例化的整个流程。构建过程的核心是通过build和等函数,逐步将配置文件解析并生成模型的各个模块对象,最终组合成一个完整的模型结构。这一过程涉及多个类和函数的调用,其中函数是生成对象的关键,它能够从配置字典中提取必要信息并生成对应的类实例。

lovely_yoshino的博客 8114

LeetCode 第523题 连续的子数组和 C语言

题目描述 523. 连续的子数组和 给你一个整数数组 nums 和一个整数k ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组: 子数组大小 至少为 2 ,且 子数组元素总和为 k 的倍数。 如果存在,返回 true ;否则,返回 false 。 如果存在一个整数 n ,令整数 x 符合 x = n * k ,则称 x 是 k 的一个倍数。 示例 1: 输入:nums = [23,2,4,6,7], k = 6 输出:true 解释:[2,4] 是一个大小为 2 ...

weixin_44916945的博客 506

微信小程序 - 微信支付报错 “调用支付JSAPI缺少参数: total_fee“,微信小程序强力解决调用支付JSAPI缺少参数:total_fee解决办法(包括微信小程序云开发对接微信支付报错问题)

调用支付jsapi缺少参数 total_fee,微信小程序拉起支付报: 调用支付JSAPI缺少参数: total_fee,微信小程序云开发支付 调用支付jsapi缺少参数:total_fee,微信小程序云开发微信支付报错,拉起支付失败,调用支付jsapi缺少参数:total_fee_小程序支付问题怎么解决?使用微信小程序云开发对接微信支付,拷贝官方的示例代码运行,详细解决方案,如何解决这个情况,后端没问题,还是依旧报错,微信小程序 微信支付 total_fee 缺失参数 云开发 统一下单 JSAPI,报错:

🏆 前端领域优质创作者 2961

LeetCode第523题 - 连续的子数组和

LeetCode第523题 - 连续的子数组和

流水账 1251

【DRAM存储器五十六】LPDDR5介绍--WCK2CK Leveling和Read gate training

简述LPDDR5 WCK2CK leveling和read gate traning基本原理与操作步骤

highman110的博客 8800

【FDTD】仿真目录(共计 16 篇)

对于完美吸收型边界条件,有时候我们需要设置将仿真结构穿过边界条件区域,具体原因可以阅读下文。在进行 S 参数扫描时,当考虑到材料的色散特性时,我们可以如下文一样选择计算群延迟。仿真,我们需要构建仿真结构与仿真区域,对于它们的尺寸,应该遵循下文进行设定。中存在多种不同的网格类型,具体可以参考下文,这部分内容仅作了解即可。尺寸的设置问题,对于 mesh 的最小尺寸,可以参考下文。的边界条件设置也十分重要,下文中我们对边界条件进行总结。仿真中我们可以设定参与仿真的模式数量,可以参考下文。尺寸的实例,详见下文。

u011699626的博客 510

LeetCode 523:连续的子数组和 | 前缀和同余定理

连续的子数组和问题展示了前缀和与同余定理结合的巧妙应用。通过将"和是 K 的倍数"转化为"两个前缀和同余",我们可以在 O(n) 时间内解决这个看似需要 O(n²) 的问题。这个问题的关键洞察是:子数组和是 K 的倍数 <=> 两个前缀和对 K 同余。利用哈希表记录每个模首次出现的位置,我们可以在遍历过程中快速判断是否存在满足条件的子数组。希望通过本文的讲解,读者能够掌握同余定理在算法问题中的应用,并将其推广到更多类似问题的解决中。

cannonjinx的博客 4431

YOLO11改进|卷积篇|引入多样化分支块DBB

YOLO11中添加多样化分支块DBB

A1983Z的博客 1450

LeetCode //C - 523. Continuous Subarray Sum

【代码】LeetCode //C - 523. Continuous Subarray Sum。

Made in Code 981

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode 523. Continuous Subarray sum和 525. Contiguous Array和560. Subarray Sum Equals K

523. Continuous Subarray Sum Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multi

老夫编代码就是一把梭 714

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode 523. Continuous Subarray Sum 解题报告

LeetCode 523. Continuous Subarray Sum 解题报告题目描述Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums

骆小坑的填坑之旅 5011

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[leetcode]-523-Continuous Subarray Sum

题目: Given a list ofnon-negativenumbers and a targetintegerk, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple ofk, that is, sums up ...

ljh0302的专栏 339

[Leetcode] 523. Continuous Subarray Sum

Problems: https://leetcode.com/problems/continuous-subarray-sum/ Solution1: 利用sum存储到第 i 位的sum 注意计算diff时subarray长度至少为2 class Solution { public boolean checkSubarraySum(int[] nums, int k) { ...

Renoler的博客 180

Leetcode-523. Continuous Subarray Sum

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发优快云,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。 博客链接:mcf171的博客 —————————————————————————————— Given a li

mcf171的专栏 1746

LeetCode: 523. Continuous Subarray Sum

LeetCode: 523. Continuous Subarray Sum

qq_16318319的博客 839

leetcode523. Continuous Subarray Sum

523. Continuous Subarray SumGiven a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple

rxt2012kc的博客 401

[leetcode] 523. Continuous Subarray Sum

Description Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is,...

Learning from the mistakes 254

[LeetCode]523. Continuous Subarray Sum

https://leetcode.com/problems/continuous-subarray-sum/#/description 找到和为K的倍数的非负数组的最长连续子数组 1、处理k为0的情况;2、用HashMap保存sum对k取余数,如果前序有余数也为sum % k的位置,那么就存在连续子数组和为k的倍数 public class Solut

gqk289的专栏 2555

LeetCode523 Continuous Subarray Sum

Check if contains a subarray that sum up equals to times of k and pay attention: all elements in given array are non-negative. this problem is pretty easy, still, I stucked on conner case which k==0 for a really long time. My codes are as follows. the time

Tech in Pieces 406

【LeetCode 523】 Continuous Subarray Sum

题目描述 Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer. Example 1: Input: [23

iCode_girl的博客 182

LeetCode 523. Continuous Subarray Sum

523. Continuous Subarray SumGiven a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple

mingjc的博客 306
上一篇: Leetcode 152. Maximum Product Subarray
下一篇: Leetcode 236. Lowest Common Ancestor of a Binary Tree
vincentCheungM
博客等级 码龄10年 4粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值