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. RowSet的使用

    ResultSet是使用Jdbc编程的人入门和常用的操作数据库的类,自 JDK1.4开始,易于使用RowSet接口被引入.RowSet 接口扩展了标准java.sql.ResultSet接口.RowS ...

  2. docker安装hadoop集群

    docker安装hadoop集群?图啥呢?不图啥,就是图好玩.本篇博客主要是来教大家如何搭建一个docker的hadoop集群.不要问 为什么我要做这么无聊的事情,答案你也许知道,因为没有女票.... ...

  3. nodejs6下使用koa2

    koa2里面使用ES7的语法,如async.await所以需要运行在node7.6之后:但在node7.6之前也可以利用babel是的koa2可以运行. 首先项目中安装babel,和babel的几个模 ...

  4. struts2.3.23升级到struts2.3.32

    新的漏洞 3月8号去审计厅培训系统的使用,那边计算机中心的负责人递过来一张如下图所示的文档,意思是发现了struts2的漏洞,需要进行修复. 在培训前,我登录到服务器中,看到了项目中,所有的服务器中应 ...

  5. Spring切面通知执行的顺序(Advice Order)

    问题描述 如果在Spring的程序中同时定义了环绕通知(Around)和前置通知(Before)..那么,有以下问题: 1.怎么让两个切面通知都起作用 2.或者让两者切面按自己指定的顺序进行执行? 3 ...

  6. Ubuntu安装中文输入法

    如果你没有因为各种尝试搞乱了Ubuntu系统的话: 1. 在All Settings 里找到 Language Support, Install/Remove Languages里安装Chinese ...

  7. Java中 EvenQueue.invokeLater用法

    在Java中Swing是线程不安全的,是单线程的设计,这样的造成结果就是:只能从事件派发线程访问将要在屏幕上绘制的Swing组件.事件派发线程是调用paint和update等回调方法的线程,它还是事件 ...

  8. js中toggle()及toggleClass()的使用详解

    在javascript中toggle()为连续点击事件,当里面含有多个function(){}函数时,每次点击依次执行里面的function的函数,直至最后一个.随后每次点击都重复对这几个函数的轮番调 ...

  9. Ajax01 什么是ajax、获取ajax对象、ajax对象的属性和方法

    1 什么是ajax ajax是一种用来改善用户体验的技术,其本质是利用浏览器提供的一个对象(XMLHttpRequest,也可称之为ajax对象) 向服务器发送异步请求;服务器返回部分数据(不是一个完 ...

  10. javaWeb学习总结(7)-关于session的实现:cookie与url重写

    本文讨论的语境是java EE servlet.我们都知道session的实现主要两种方式:cookie与url重写,而cookie是首选(默认)的方式,因为各种现代浏览器都默认开通cookie功能, ...