[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 ...
随机推荐
- 机器学习:从编程的角度理解BP神经网络
1.简介(只是简单介绍下理论内容帮助理解下面的代码,如果自己写代码实现此理论不够) 1) BP神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...
- Java IO详解(五)------包装流
File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...
- Gitlab使用Webhook实现Push代码自动部署
1.Jenkins 安装完成以后,首先我们在Jenkins中需要安装一下,Gitlab Hook Plugin 插件: 2.插件安装完成我们创建任务,在任务重构建触发器下获取回调URL: 注意: 注意 ...
- Spring-web中的web.xml为Servlet提供的配置选项说明
配置Servlet时可以设置的一些初始化参数,总结如下: ContextAttribute: 在ServletContext的属性中,要用作WebApplicationContext的属性名称. Co ...
- JBoss7 如何用脚本 启动 和 停止
用脚本来启动/停止JBoss服务器,有助于开发部署的 自动执行,提高工作效率. 在JBoss以前的版本中,很容易在bin目录下面找到 启动和停止服务器的脚本: run.bat shutdown.bat ...
- zabbix agent安装详解
安装 Installing repository configuration package Zabbix 2.2 for RHEL5, Oracle Linux 5, CentOS 5: rpm - ...
- 小程序API录音后Silk格式转码MP3
问题 客户端使用小程序,需要录音功能然后到后台页面播放,由于微信提供的录音API压缩后的格式为 .silk格式的,但是这个格式其他播放器都是播放不了的,更何况html页面的audio标签更是不可能播放 ...
- spring 事务无效解决方法
(原) spring 事务目前有二种,注解式和声明式,以前都是以公司里的框架写好的,没有学习的机会,今天抽空好好试了下,结果遇到好多问题. 1.注解式 最开始是这么玩的,发现数据进数据库了,没有起作用 ...
- zepto源码分析系列
如果你也开发移动端web,如果你也用zepto,应该值得你看看.有问题请留言. Zepto源码分析-架构 Zepto源码分析-zepto(DOM)模块 Zepto源码分析-callbacks模块 Ze ...
- 第二天0605下午——超链接<a>与图片<img>
今天下午学习了超链接<a>标签和图片<img>标签,以下面代码为例: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...