[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 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.
思路:
首先想到用动态规划,使用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]的更多相关文章
- [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 ...
- 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题是存储的当前累加和的 ...
- 523. Continuous Subarray Sum是否有连续和是某数的几倍
[抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...
- 【leetcode】523. Continuous Subarray Sum
题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...
- 523 Continuous Subarray Sum 非负数组中找到和为K的倍数的连续子数组
非负数组中找到和为K的倍数的连续子数组 详见:https://leetcode.com/problems/continuous-subarray-sum/description/ Java实现: 方法 ...
- 523. Continuous Subarray Sum
class Solution { public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_map&l ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
随机推荐
- 开启MongoDB客户端访问控制
参考官方文档:https://docs.mongodb.org/v2.6/tutorial/enable-authentication/ 基于版本:MongoDB 2.6 概览 在MongoDB数据实 ...
- Centos6.5_x86上Oracle11g2 32位的安装与卸载以及相关问题汇总
需要注意的问题: 1.Linux包括内核和要安装的oralce版本是否符合(这个在官方文档中有说明). 2.安装oracle一般会新建一个为oracle的账户,注意在安装的过程中的root和oracl ...
- Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析
原文发表于 2013 年 8 月 28 日 由 三石 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给出了Fork Join的实现.在Java SE 7的API中,多了ForkJoin ...
- 又是一个MVP+RxJava+Retrofit的干货集中营
欢迎访问我的个人博客 ,未经允许不得转发 前言 今天想要与大家一起分享的是近一个星期内开发的一个新app--干货集中营客户端,其实网上已经有许多类似的项目,代码家也在他的干货集中营中推荐了几款优秀的作 ...
- js导出CSV
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- Ubuntu安装Cassandra
Uninstall Cassandra $ sudo su remove cassandra $ apt-get remove cassandra cleaned the cassandra fold ...
- 【JAVAWEB学习笔记】28_jqueryAjax:json数据结构、jquery的ajax操作和表单校验插件
Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用 一.json数据结构 1.什么是json JSON(J ...
- 面试(2)-java-se-HashSet和TreeSet
Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...
- Linux系统下安装Mysql5.7.18教程收集分享
本人最近服务器新手入门,需要搭建一个在linux虚拟机上的服务器 第一天再装虚拟机,选的linux系统CentOS,一切顺利. 第二天,要给虚拟机装Mysql,但是需要用到命令行进行安装/操作等,我是 ...
- 一个Monad的不严谨介绍
一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已,这有什么难以理解的?* 之前了解了下Monad,后来一段时间没碰,最近研究Parser用到Monad时发现又不懂了.现在重新折腾,趁着 ...