思路:

令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. Python标准库:内置函数tuple([iterable])

    本函数实现从可迭代对象生成一个元组对象返回.元组对象是一个不可改动的列表对象. 样例: #tuple() print(tuple([1, 2, 3])) print(tuple((1, 2, 3))) ...

  2. SaltStack学习系列之自定义grains

    Master端 打开存放自定义grains的目录 vim /etc/salt/master file_roots: base: - /srv/salt/ 建立自定义模块 cd /srv/salt mk ...

  3. Swift 函数Count,Filter,Map,Reduce

    原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 前言:和OC不同,Swift有非常多全局的函数,这些全局函数对简化代码来说非常实用.眼下Swift出到了2.0,只是我这篇 ...

  4. 最简单 NDK 样例

    以下在 Ubuntu下 编译一个 c 语言 hello world 并在 android 手机或模拟器上执行 进入程序位置 cd  ~/pnp5/jni 有三个文件 main.c Android.mk ...

  5. Codeforces 455B A Lot of Games 字典树上博弈

    题目链接:点击打开链接 题意: 给定n个字符串,k局游戏 对于每局游戏,2个玩家轮流给一个空串加入一个小写字母使得加完后的字符串不是n个字符串的前缀. 输家下一轮先手 问是先手必胜还是后手必胜 思路: ...

  6. web 开发之js---JS变量也要注意初始化

    原先以为js作为弱类型语言,变量的初始化没必要,但是: var text; text+="你好"; alert(text); 对话框弹出的内容是:"undefined你好 ...

  7. 【Espruino】NO.13 蓝牙模块

    http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...

  8. HDU 4445 数学-抛物运动

                                                          D - Crazy Tank                                 ...

  9. BestCoder Round #56 /hdu5464 dp

    Clarke and problem 问题描述 克拉克是一名人格分裂患者.某一天,克拉克分裂成了一个学生,在做题. 突然一道难题难到了克拉克,这道题是这样的: 给你nn个数,要求选一些数(可以不选), ...

  10. 【bzoj1260】[CQOI2007]涂色paint

    题意:就是说一开始一个序列是空的,然后每次可以将连续的一段染成同一颜色,问多少次才能到目标状态. 一开始想的是二分,然后题解DP... f[i][j]表示区间[i,j]需要染色多少次 首先初始状态是f ...