【leetcode】523. Continuous Subarray Sum
题目如下:

解题思路:本题需要用到这么一个数学定理。对于任意三个整数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的更多相关文章
- 【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 ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 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题是存储的当前累加和的 ...
- [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 ...
- 523. Continuous Subarray Sum是否有连续和是某数的几倍
[抄题]: Given a list of non-negative numbers and a target integer k, write a function to check if the ...
- 【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 ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【数组】Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
随机推荐
- OpenStack Nova 高性能虚拟机之 CPU 绑定
目录 文章目录 目录 前文列表 KVM KVM 的功能列表 KVM 工具集 KVM 虚拟机的本质是什么 vCPU 的调度与性能问题 Nova 支持的 vCPU 绑定 vcpu\_pin\_set 配置 ...
- delphi 每英寸相素点取值偏差
在所有资料中,每英寸相素点之比一般是这两个值,即:0.0393700788 25.399999961392 但是在GDI编程中,却遇到LOGPIXELSX LOGPIXELSY 在取值为96DPI ...
- 使用python脚本部署mariadb主从架构
环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. grant all on *.* to root@'%' iden ...
- lambda一些查询语句
<!--得分数据结构-->1 <Score> <studentid>1</studentid> <courseid>1</course ...
- 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)
[AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...
- Jackson常用注解及用法
最近写项目,用到Jackson的一些注解,总结一下,帮助自己记忆. 1.@JsonIgnore 和 @JsonIgnoreProperties 两个注解可以对照比较后选择使用: @JsonIgnore ...
- Django-DRF组件学习-其他学习
1.认证Authentication 可以在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_ ...
- Spoj 2798 Qtree3
一棵结点为黑色或白色的树,初始都是白色的.有两种操作 1 将一个结点换颜色 2 询问从根到结点u路径上面的第一个黑色点,没有则输出-1 InputIn the first line there are ...
- pymysql操作数据库
pymysql.connect()参数说明:(连接数据库时需要添加的参数)host(str): MySQL服务器地址port(int): MySQL服务器端口号user(str): 用户名passwd ...
- centos 7 无网络情况下,解决yum 安装依赖rpm包
方法一:在一台有网络的机器,用yum下载好所需程序,传到另外一台网络的机器上安装 yum install xtrabackup --downloadonly --downloaddir=/rpmpat ...