Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 468f8bd

Browse files
2020年10月05日
1 parent cbbc73c commit 468f8bd

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def findLength(self, A, B):
3+
"""
4+
:type A: List[int]
5+
:type B: List[int]
6+
:rtype: int
7+
"""
8+
# dp[i][j] represents A[:i + 1], B[:j + 1] longest common subarray length
9+
dp = [[0 for _ in range(len(B) + 1)] for _ in range(len(A) + 1)]
10+
11+
res = 0
12+
13+
for i in range(1, len(A) + 1):
14+
for j in range(1, len(B) + 1):
15+
if A[i - 1] == B[j - 1]:
16+
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1)
17+
res = max(dp[i][j], res)
18+
# print dp
19+
return res

‎0811.子域名访问计数/0811-子域名访问计数.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ def subdomainVisits(self, cpdomains):
44
:type cpdomains: List[str]
55
:rtype: List[str]
66
"""
7-
resList = []
8-
resMap = {}
9-
for s in cpdomains:
10-
count, domains = s.split(' ')
11-
n = domains.count('.')
12-
tmp = domains
13-
for i in range(n+1):
14-
if resMap.has_key(tmp):
15-
resMap[tmp] = resMap[tmp] + int(count)
16-
else:
17-
resMap[tmp] = int(count)
18-
index = tmp.find('.') + 1
19-
if index == -1:
20-
break
21-
else:
22-
tmp = tmp[index:]
23-
# for key, value in resMap.items():
24-
# resList.append(str(value) + ' ' + key);
25-
# return resList
26-
return [str(resMap[key]) + ' ' + key for key in resMap]
7+
from collections import defaultdict
8+
dic = defaultdict(int)
9+
10+
for pair in cpdomains:
11+
splitted_pair = pair.split()
12+
cnt, domain = splitted_pair[0], splitted_pair[1]
13+
cnt = int(cnt)
14+
15+
for i in range(len(domain)):
16+
if not i or domain[i] == ".":
17+
dic[domain[i:].lstrip(".")] += cnt
18+
19+
res = []
20+
for domain, frequency in dic.items():
21+
res.append(" ".join([str(frequency), domain]))
22+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /