当前位置:网站首页>523. Continuous Subarray Sum
523. Continuous Subarray Sum
2022-06-23 08:24:00 【ujn20161222】
523. Continuous Subarray Sum
Medium
1647228Add to ListShare
Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.
An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.
Example 1:
Input: nums = [23,2,4,6,7], k = 6 Output: true Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Example 2:
Input: nums = [23,2,6,4,7], k = 6 Output: true Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
Example 3:
Input: nums = [23,2,6,4,7], k = 13 Output: false
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1090 <= sum(nums[i]) <= 231 - 11 <= k <= 231 - 1
Accepted
277,174
Submissions
1,024,226
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
d=defaultdict(list)
for i, sm in enumerate([0]+list(accumulate(nums))):
d[sm%k]+=[i]
for val in d:
if d[val][-1]-d[val][0]>=2: return True
return Falsed中存的是余数对应的索引
之后再判断间隔就好
边栏推荐
- Derivation and loading of the trained random forest model
- usb peripheral 驱动 - configfs
- Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
- Why do we say that the data service API is the standard configuration of the data midrange?
- 4- draw ellipse, use timer
- 词性家族
- Lighthouse cloud desktop experience
- What are open source software, free software, copyleft and CC? Can't you tell them clearly?
- (resolved) difference between leftmost prefix and overlay index
- 高通9x07两种启动模式
猜你喜欢

Why do we say that the data service API is the standard configuration of the data midrange?

十多年前的入职第一天

Use of tensorboard

Linux Mysql安装

Why use growth neural gas network (GNG)?

How to start Jupiter notebook in CONDA virtual environment

目标检测中的多尺度特征结合方式

Basic use of check boxes and implementation of select all and invert selection functions

Multi-scale feature combination in target detection

Monitor the cache update of Eureka client
随机推荐
List接口三个子实现类
How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
Open source stealing malware mercurial found in the field for "educational purposes"
通信方式总结及I2C驱动详解
7-调色板-CALayer和触摸
How to mine keywords and improve user experience before website construction?
Driver Architecture & platform platform bus driver model
实战监听Eureka client的缓存更新
训练后的随机森林模型导出和加载
1-渐变、阴影和文本
Derivation and loading of the trained random forest model
Does huangrong really exist?
How to start Jupiter notebook in CONDA virtual environment
史上最污技术解读,60 个 IT 术语我居然秒懂了......
Vulnhub | dc: 3 | [actual combat]
Generate code 39 extension code in batch through Excel file
[cross border e-commerce solutions] lighthouse will be used for pure IP expansion of new business - continuous efforts!
Go data types (II) overview of data types supported by go and Boolean types
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?