Skip to content

Commit

Permalink
2020-02-13
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Feb 14, 2020
1 parent 845c31d commit faa4775
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
6 changes: 1 addition & 5 deletions 0551.学生出勤记录I/0551-学生出勤记录I.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ def checkRecord(self, s):
:type s: str
:rtype: bool
"""
if s.count("A") > 1 or "LLL" in s:
return False

return True

return s.count("A") <= 1 and "LLL" not in s
23 changes: 13 additions & 10 deletions 0560.和为K的子数组/0560-和为K的子数组.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ def subarraySum(self, nums, k):
:type k: int
:rtype: int
"""
# prefix[i] = sum(nums[:i])
# prefix[j] - prefix[i] = sum(nums[i:j])
from collections import defaultdict
pre_sum = 0
record = defaultdict(int)
record[0] = 1

prefix = [0 for _ in range(len(nums) + 1)]

for i, x in enumerate(nums):
prefix[i + 1] = prefix[i] + x

dic = defaultdict(int)
res = 0
for i in range(len(nums)):
pre_sum += nums[i]

#ÕÒ k - pre_sum
res += record[pre_sum - k]
record[pre_sum] += 1

for i, x in enumerate(prefix):
res += dic[x - k]
dic[x] += 1

return res

0 comments on commit faa4775

Please sign in to comment.