题目如下:

解题思路:本题需要用到这么一个数学定理。对于任意三个整数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. .NET中使用EF6与连接MYSQL

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,不仅支持SQL Server,还支持MySQL.Ora ...

  2. 使用事件CreateEvent注意事项 多进程同步的方法

    https://www.cnblogs.com/aakuang/p/3514658.html 使用事件CreateEvent注意事项   HANDLECreateEvent( LPSECURITY_A ...

  3. 安装iamp模块,编译报错configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.

    yum install libc-client-devel cd /root/lnmp1.0-full/php-5.3.17/ext/imap /usr/local/php/bin/phpize ./ ...

  4. 递归算法之排列组合-求一个集合S的m个元素的组合和所有可能的组合情况

    求一个集合S的m个元素组合的所有情况,并打印出来,非常适合采用递归的思路进行求解.因为集合的公式,本身就是递归推导的: C(n,m) = C(n-1,m-1) + C(n-1,m). 根据该公式,每次 ...

  5. 把对像生成json并存储到文件

    1.创建实体对像json import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class S ...

  6. 剑指Offer编程题(Java实现)——两个链表的第一个公共结点

    题目描述: 输入两个链表,找出它们的第一个公共结点. 思路一: 设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a. ...

  7. Android使用adb抓完整Log

    前言     最新项目里一直在做 Android RIL 方面的研究,非常最终项目还是未能解决通信底层模块的问题,但是在使用adb抓log上还是有一些收获的,这里记录一下.   Log分类     A ...

  8. 《剑指offer》面试题27 二叉搜索树与双向链表 Java版

    (将BST改成排序的双向链表.) 我的方法一:根据BST的性质,如果我们中序遍历BST,将会得到一个从小到大排序的序列.如果我们将包含这些数字的节点连接起来,就形成了一个链表,形成双向链表也很简单.关 ...

  9. 题解 CF1119A 【Ilya and a Colorful Walk】

    此题就是:给你一个数组,让你找出两个不同的元素,并让它们的下标差距最大. 思路:从2到n,如果与1不同,记录距离,与原数比较,取大. 从1到n-1,如果与n不同,记录距离,与原数比较,取大. AC代码 ...

  10. yaf框架安装

    第一步:明白yaf框架是以扩展的形式要先配置到php里面,对于windows系统的使用者,首先要去官网:http://code.google.com/p/yafphp/downloads/list如果 ...