题目如下:

解题思路:本题需要用到这么一个数学定理。对于任意三个整数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. Python Module_Socket_网络编程

    目录 目录 Socket 套接字 套接字的原理 套接字的数据处理方式 套接字类型 Socket 标准函数 ServerSocket 标准函数 ClientSocket 标准函数 公有标准函数 Sock ...

  2. robotframework(rf)中对时间操作的datetime库常用关键字

    1.对固定日期进行操作,增加或减去单位时间或者时间段 2.对两个时间段进行操作 3.对时间格式转化,获取时间戳. 4.从完整时间中取指定年月日等 5.对时间类型进行格式化 6.获取当前时间或者指定时区 ...

  3. Apache配置文件介绍

    一.配置文件存放位置 apache配置文件名为httpd.conf 1.yum安装 yum安装后,apache配置文件httpd.conf存放在目录/etc/httpd/conf下 2.源码编译安装 ...

  4. C# 无焦点获取扫码枪扫码信息

    代码网上有的是,多是需要窗体焦点直接show出扫码信息(usb,模拟键盘,hook) 怎样才能真的无焦点获取? 用串口方式 usb转串口 以接收串口通讯消息的方式获取扫码信息

  5. js实现上传文件实时显示缩略图

    <input name="coverImage" onClick="" onchange="setImagePreview(this);&quo ...

  6. Babel编译:类

    编译前 class Fruit{ static nutrition = "vitamin" static plant(){ console.log('种果树'); } name; ...

  7. 【MM系列】SAP 各种冲销凭证

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 各种冲销凭证   前言部分 ...

  8. 03Java面试题-------------中科软

    1.String是最基本的数据类型吗?String和StringBuffer的区别? 不是.Java中的基本数据类型只有8个:byte,short,int,long,float,double,char ...

  9. Hand on Machine Learning第三章课后作业(2):其余小练习

    -#!/usr/bin/env python -# # # -- coding: utf-8 -- -# # # @Time : 2019.5.22 14:09 -# # # @Author : An ...

  10. 【Qt开发】V4L2 API详解 背景知识 打开设备设置参数

    www.linuxtv.org下,有篇文档详细讲解了V4L2相关知识和体系结构.是V4L2方面最全面的文档.可以通过它学习V4L2的一些思路和想法. http://www.linuxtv.org/do ...