博客
关于我
leetcode------523. 连续的子数组和[1]
阅读量:186 次
发布时间:2019-02-28

本文共 1201 字,大约阅读时间需要 4 分钟。

题目描述:

给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数

示例 1:

输入: [23,2,4,6,7], k = 6输出: True解释: [2,4] 是一个大小为 2 的子数组,并且和为 6。

示例 2:

输入: [23,2,6,4,7], k = 6输出: True解释: [23,2,6,4,7]是大小为 5 的子数组,并且和为 42。

说明:

  1. 数组的长度不会超过10,000。
  2. 你可以认为所有数字总和在 32 位有符号整数范围内。

关键是找到转移方程,让以前累计的有用。

另外注意的是不需要N行,边计算边验证,只需要2行即可。否则无法开数组。

class Solution {    public:        int D[2][10000];        bool checkSubarraySum(vector
& nums, int k) { int N=nums.size(); int kk=N; int ilevel=0; if(N==0) return false; //if(k==0) return false; for(int i=0; i
0) { for(int i=0; i

或者采用穷举法,通过预处理 复杂性(n^2)

class Solution {    public:        int sum[10010];        bool checkSubarraySum(vector
& nums, int k) { int N=nums.size(); int kk=N; int ilevel=0; if(N==0) return false; nums.push_back(0); //if(k==0) return false; int isum=0; for(int i=N; i>-1; i--) { isum+=nums[i]; sum[i]=isum; } for(int i=0; i<=N; i++) for(int j=0; j

 

转载地址:http://vnki.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>