思路:

令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的更多相关文章

  1. [LintCode] Continuous Subarray Sum II

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

  2. LintCode 402: Continuous Subarray Sum

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

  3. Continuous Subarray Sum II(LintCode)

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

  4. 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题是存储的当前累加和的 ...

  5. [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 ...

  6. [LintCode] Continuous Subarray Sum 连续子数组之和

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

  7. Continuous Subarray Sum

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

  8. [LeetCode] Continuous Subarray Sum 连续的子数组之和

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

  9. Continuous Subarray Sum LT523

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

随机推荐

  1. TCP打洞与UDP打洞的差别

    为什么网上讲到的P2P打洞基本上都是基于UDP协议的打洞?难道TCP不可能打洞?还是TCP打洞难于实现?     如果如今有内网clientA和内网clientB.有公网服务端S.     如果A和B ...

  2. 关于C/S架构系统的安全监测

    由于工作需求,需要对一大批C/S架构的系统进行测试,所以这几天一直在摸索怎么个套路法,踩过的坑就不发了,直接奔我个人的套路: C/S架构的系统,说最直白一点就是一堆.exe的系统,他们大部分没有web ...

  3. 条款八: 写operator new和operator delete时要遵循常规

    自己重写operator new时(条款10解释了为什么有时要重写它),很重要的一点是函数提供的行为要和系统缺省的operator new一致.实际做起来也就是:要有正确的返回值:可用内存不够时要调用 ...

  4. Mybatis的配置文件和映射文件具体解释

    一.Mybatis的全局配置文件 1.SqlMapConfig.xml是mybatis的全局配置文件.配置内容例如以下: properties(属性) settings(全局配置參数) typeAli ...

  5. OSX:不同OSX版本号的标记可能不兼容

    现象: 依据測试,中文OS X 10.9和中文10.10的文件标记彼此不兼容. 也就是说.比方在10.9中的颜色标记,在10.10DP2中不能删除,但能够加入/删除10.10自己的颜色标记,反之亦然. ...

  6. java struts jxl 导入导出Excel(无模板)

    jar包: import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.io.Fil ...

  7. 【bzoj1034】[ZJOI2008]泡泡堂BNB

    贪心 将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜利,若无法做到,则暂时不考虑给对方最强的选手匹配对手.第二遍扫描使用同样策略去获取尽量 ...

  8. mysql对表列数和行大小的限制

    mysql对表列数和行大小的限制 - CSDN博客 https://blog.csdn.net/Dr_Joseph/article/details/78111312

  9. 20170626_oracle_数据库设计

    数据库设计的定义:规划数据库中数据对象以及之间关系的过程. 为什么进行数据库设计? 空间 完整性 程序开发 数据库设计前提知识: 范式: 1NF:第一范式 第一范式的目标是确保每列的原子性 如果每列都 ...

  10. 解决jQuery uploadify在非IE核心浏览器下无法上传

    之前上传了一个通过Flash实现多文件上传,但是在IE正常运行,FireFox 不能正常上传.经过反复研究学习,之所以firefox和360浏览器无法正常运行,是因为FireFox.chrome.36 ...