题目如下:

解题思路:本题需要用到这么一个数学定理。对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0。利用这个定理,我们可以对数组从头开始进行求和,同时利用字典保存余数(key:余数,value:最早出现这个余数的元素下标),每累加一个元素都对k取余数,如果余数在字典中存在并且两个元素之间的下标差大于等于2,即表示存在这样的subarray。而对于k=0的情况,必须要出现至少两个出现至少连续两个0才能符合条件;当然,因为0*k = 0,所以如果数组中连续出现两个0,对任意的k都是满足条件的。

代码如下:

class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
continuousZero = False
for i in xrange(len(nums) - 1):
if nums[i] + nums[i + 1] == 0:
continuousZero = True
break
if continuousZero == True:
return True
elif k == 0:
return False dic = {}
amount = 0
for i, v in enumerate(nums):
amount += v reminder = amount % k
if reminder == 0 and v != amount:
return True
if reminder in dic :
if i - dic[reminder] >= 2:
return True
else :
dic[reminder] = i
return False

【leetcode】523. Continuous Subarray Sum的更多相关文章

  1. 【leetcode】Minimum Size Subarray Sum(middle)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

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

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

  5. 523. Continuous Subarray Sum是否有连续和是某数的几倍

    [抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...

  6. 【leetcode】1191. K-Concatenation Maximum Sum

    题目如下: Given an integer array arr and an integer k, modify the array by repeating it k times. For exa ...

  7. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  8. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

  9. 【数组】Minimum Size Subarray Sum

    题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...

随机推荐

  1. linux下搭建mysql数据库

    linux下搭建mysql数据库 1.下载mysql: http://dev.mysql.com/downloads/mysql/5.6.html#downloads wget http://dev. ...

  2. JS刷新后回到页面顶部

    window.location.href = location.href; 方法一: $(window).scrollTop(0); 方法二:$('html ,body').animate({ scr ...

  3. GCC 编译参数

    -s 这个参数会把符号表从最终的可执行文件中删除.没有符号表,你就不能用gdb调试了,但是程序会更小 -O0 不做任何优化,这是默认的编译选项 -c 只编译不链接,产生.o文件,就是obj文件,不产生 ...

  4. [Web 前端] 028 jQuery 事件

    目录 jQuery 的事件 1. 事件绑定 1.1 事件的获取 1.2 基本绑定 1.3 动态绑定 2. 事件触发 2.1 触发的写法 2.2 常用的鼠标事件 3. 事件冒泡和默认行为 3.1 事件冒 ...

  5. 【监控笔记】【1.2】监控事件系列——SQL Trace(默认跟踪与自定义跟踪)

    目录: [1.1]概念与使用 [1.2]跟踪的基本操作 --[1.2.1]查看默认跟踪是否在运行 --[1.2.2]开启默认跟踪 --[1.2.3]关闭默认跟踪 --[1.2.4]查看跟踪文件/查看跟 ...

  6. P5020货币系统

    这个题是2018提高组真题,是一道看不出是背包的背包题. 题干特别长,甚至有些没看懂.题意为给出一组货币面值,然后从这里面用最少的面值数量取代原先的面值.比如3,6直接用3表示.一开始想到了小凯的疑惑 ...

  7. 7.golang的字符串 string

    golang 字符串为不可变的量 ,字符串定义要使用双引号 package main import "fmt" func main() { var xx string = 'xxx ...

  8. HNUSTOJ 1601:名字缩写

    1601: 名字缩写 时间限制: 1 Sec  内存限制: 128 MB 提交: 288  解决: 80 [提交][状态][讨论版] 题目描述 Noname老师有一个班的学生名字要写,但是他太懒了,想 ...

  9. P3198 [HNOI2008]遥远的行星

    传送门 发现 $A$ 不大,又允许较大的误差,考虑乱搞 考虑求出每个位置的答案,因为有 $1e5$ 个位置,所以每个位置差不多可以计算 $100$ 次贡献 所以把每个可以贡献的位置尽量均匀分成 $10 ...

  10. 源码编译Redis Desktop Manager | 懒人屋

    原文:源码编译Redis Desktop Manager | 懒人屋 源码编译Redis Desktop Manager  2.3k  字    10  分钟    2019-10-10 文章背景 本 ...