-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path523-CheckSubarraySum.py
More file actions
105 lines (95 loc) · 3.05 KB
/
523-CheckSubarraySum.py
File metadata and controls
105 lines (95 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Python3
from typing import List
# 超时
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n < 2: # 题目要求子数组大小至少为 2
return False
dp = [[num for num in nums] for i in range(n-1)]
for i in range(n-1):
for j in range(i+1, n):
dp[i][j] += dp[i][j-1]
if dp[i][j] % k == 0:
return True
return False
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
n = len(nums)
if n < 2:
return False
mp = dict()
mp[0] = -1
remainder = 0
for i in range(n):
remainder = (remainder + nums[i]) % k
if remainder in mp:
preIndex = mp[remainder]
if i - preIndex >= 2:
return True
else:
mp[remainder] = i
return False
if __name__ == "__main__":
test = Solution()
# nums = [23,2,6,4,7]
# k = 13
nums = [0]
k = 1
res = test.checkSubarraySum(nums, k)
print(res)
# 前缀和 + 哈希表
# 事先计算出数组 nums 的前缀和数组,则对于任意一个子数组,都可以在 O(1) 的时间内得到其元素和。
# 当 prefixSums[q]−prefixSums[p] 为 k 的倍数时,prefixSums[p] 和 prefixSums[q] 除以 k 的余数相同。
# 由于哈希表存储的是每个余数第一次出现的下标,因此当遇到重复的余数时,根据当前下标和哈希表中存储的下标
# 计算得到的子数组长度是以当前下标结尾的子数组中满足元素和为 k 的倍数的子数组长度中的最大值。
# 只要最大长度至少为 22,即存在符合要求的子数组。
# Java
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
int m = nums.length;
if (m < 2) {
return false;
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, -1);
int remainder = 0;
for (int i = 0; i < m; i++) {
remainder = (remainder + nums[i]) % k;
if (map.containsKey(remainder)) {
int prevIndex = map.get(remainder);
if (i - preIndex >= 2) {
return true;
}
} else {
map.put(remainder, i);
}
}
return false;
}
}
# C++
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int m = nums.size();
if (m < 2) {
return false;
}
unordered_map<int, int> mp;
mp[0] = -1;
int remainder = 0;
for (int i = 0; i < m; i++) {
remainder = (remainder + nums[i]) % k;
if (mp.count(remainder)) {
int preIndex = mp[remainder];
if (i - preIndex >= 2) {
return true;
}
} else {
mp[remainder] = i;
}
}
return false;
}
};