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:

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

思路:

首先想到用动态规划,使用dp[i][j]记录nums中i到j的和,然后随时判断是否满足余数为0.

注意处理k==0时候的情况。

但是直接用二维数组记录的话,提示内存不足,耗费空间。

更新公式为 dp[i][j] = dp[i][j-1]+nums[j] ,可以看出dp[i][j]只与上一个dp[i][j-1]有关,

于是用两个变量替代即可。得到如下代码,时间复杂度为O(n2).

bool checkSubarraySum(vector<int>& nums, int k)
{
int len = nums.size();
// vector<vector<int>> dp(len, vector<int>(len, 0));
long long cur = , pre = ;
for (int i = ; i < len;i++)
{
for (int j = i; j < len;j++)
{
if (j == i)cur = nums[i];
else
{
cur = pre + nums[j];
if (k != && cur % k == )return true;
else if (k == && cur == ) return true;
}
pre = cur;
}
}
return false;
}

后来参考网上大牛的代码,学习到了他们的解法。

比如他们用一个set去存储i之前元素和的余数,如果往后遍历到j元素和的余数之前出现过,说明i到j的和为k的整数倍。

这样时间复杂度为O(n).

class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size(), sum = , pre = ;
unordered_set<int> modk;
for (int i = ; i < n; ++i) {
sum += nums[i];
int mod = k == ? sum : sum % k;
if (modk.count(mod)) return true;
modk.insert(pre);
pre = mod;
}
return false;
}
};

参考:

https://discuss.leetcode.com/topic/80892/concise-c-solution-use-set-instead-of-map

[leetcode-523-Continuous Subarray Sum]的更多相关文章

  1. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  2. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  3. 523. Continuous Subarray Sum是否有连续和是某数的几倍

    [抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...

  4. 【leetcode】523. Continuous Subarray Sum

    题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...

  5. 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组

    非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...

  6. 523. Continuous Subarray Sum

    class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...

  7. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  8. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  10. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

随机推荐

  1. linux vi 报错 E37: No write since last change (add ! to override)

    用 vi 命令编辑文本文件,没有文件写入权限的时候会报这个错.:q   :wq   怎么都不能退出. 这时只需 ctrl+z 即可,或者在退出命令后加 ! 忽略提示     :q!

  2. 基于腾讯云的Centos6.2系统搭建Apache+Mysql+PHP开发环境

    搭建环境,我肯定需要先购买腾讯云服务器的哦! 然后,我们打开SecureCRT 7.3,这是一款可以连接Linux系统的客户端工具,使用的很方便快捷,要注意的是,若你是Linux系统的就要用22端口, ...

  3. 学习笔记TF011:多层神经网络

    线性回归.对数几率回归模型,本质上是单个神经元.计算输入特征加权和.偏置视为每个样本输入特征为1权重,计算特征线性组合.激活(传递)函数 计算输出.线性回归,恒等式(值不变).对数几率回归,sigmo ...

  4. 学习笔记TF012:卷积网络简述

    ImageNet http://www.image-net.org ,图像标注信息数据库.每年举办大规模视觉识别挑战赛(ILSVRC).基于ImageNet数据库构建完成目标自动检测分类任务系统.20 ...

  5. aws上redhat安装redis服务记

    1.准备 官网下载

  6. (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1

    Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,这里一起回顾一下Protobuf    的优点.(1)在谷歌内部长期使用,产品成熟度高:(2)跨语言,支持 ...

  7. 关于win10和sqlserver的兼容性

    本人主要是用.NET开发一些MIS(信息管理系统)系统,如酒店管理系统,医院管理系统,以及其附属的如餐饮管理系统,洗浴管理系统,以及医保管理系统,合疗管理系统,前期开发的产品主要是VS2008+sql ...

  8. mybatis xml配置文件要点说明

    mapper映射方式: 1 一一具体列举的方式 2扫描package 如: <mappers> <!-- 告知映射文件方式1,一个一个的配置 <mapper resource= ...

  9. MyEclipse使用技巧详解

    MyEclipse使用技巧的掌握是和我们开发效率挂钩的,那么如何掌握MyEclipse使用技巧呢?这里向你详细介绍了几种使用技巧的操作方法. 在了解MyEclipse使用技巧之前我们来看看MyEcli ...

  10. 数据库插入数据失败,log提示不能将值 NULL 插入列 'id'

    已经记不住具体的log信息了,意思就是ID如果没有设置为自增长的情况下就不能插入数据,而建表时ID字段是设置为"not null",所以就不能顺利插入数据. 解决方法有两种: ①建 ...