leetcode523 Continuous Subarray Sum
思路:
令sum[p]表示p位置的前缀和。如果sum[i] % k == sum[j] % k (j - i > 1),则存在子段(i, j]的和能够整除k。
实现:
class Solution
{
public:
bool checkSubarraySum(vector<int>& nums, int k)
{
if (nums.size() < ) return false;
int n = nums.size();
for (int i = ; i < n - ; i++)
if (nums[i] == && nums[i + ] == )
return true;
if (k == ) return false;
int sum = ;
unordered_map<int, int> m;
m[] = -;
for (int i = ; i < n; i++)
{
sum = (sum + nums[i]) % k;
if (m.count(sum))
{
if (i - m[sum] > )
return true;
}
else m[sum] = i;
}
return false;
}
};
leetcode523 Continuous Subarray Sum的更多相关文章
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- 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 ...
- 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题是存储的当前累加和的 ...
- [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- Continuous Subarray Sum LT523
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- pycharm下运行和调试scrapy项目
1. 新建项目 默认在本地已经新建了一个scrapy爬虫项目 2. 打开项目 点击open à 选择刚刚那个本地的scrapy项目meijutt100 3. 项目结构 各个py文件的作用不作介绍,不懂 ...
- Use Elasticksearch to solve TOP N issue
The raw data is like timestamp, router, interface, src_ip, dst_ip, protocol, pkts 10000000, 1.1.1.1 ...
- samba服務器下文件夾chmod權限技巧
需要的效果: samba下文件夹(abc)不可以被重命名.不可以被刪除,其所有子目录可读可写. 如何做到: chmod 777 -R abc # -R 使得abc下所有数据继承可读可写权限 chm ...
- web面试集合
在JavaScript中,添加到页面上的事件处理程序数量将直接关系到页面的整体运行性能.导致这一问题的原因是多方面的.首先,每个函数都是对象,都会占用内存:内存中的对象越多,性能就越差.其次,必须事先 ...
- Struts2默认拦截器栈及内建拦截器使用具体解释
Struts2内建拦截器介绍: alias (别名拦截器):同意參数在跨越多个请求时使用不同别名,该拦截器可将多个Action採用不同名字链接起来,然后用于处理同一信息. autowiring ...
- ASP.NET MVC Model之二模型绑定
Asp.net mvc中的模型绑定,或许大家经常用,但是具体说他是怎么一回事,可能还是会有些陌生,那么,本文就带你理解模型绑定.为了理解模型绑定,本文会先给出其定义,然后对通过比,来得出使用模型绑定的 ...
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- ubutu14.04无法使用sudo,也无法切换到root用户去解决问题怎么办?
一不小心,修改了/etc/sudoers文件. 惨了. 无法使用sudo了,啥都干不成了. 最最关键的是,也无法用root登录. 本想着要重装系统了. 后来发现了神奇的ubuntu安全模式. 1.重启 ...
- 一位ACMer过来人的心得(转)
刻苦的训练我打算最后稍微提一下.主要说后者:什么是有效地训练? 我想说下我的理解.很多ACMer入门的时候,都被告知:要多做题,做个500多道就变牛了.其实,这既不是充分条件.也不会是必要条件. 我觉 ...
- 转 source insight 复制后光标在前面
source insight 里编辑的时候,每次粘贴后,光标停留在粘贴内容的前面. 我想把它设定为 粘贴后,光标移动倒粘贴内容的后面. 怎么做? 这是个设置问题,按照下面的步骤设定就可以了. Opti ...