Skip to content

Commit 0d4a198

Browse files
committed
2019-07-17
1 parent 4a9f3d0 commit 0d4a198

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def findRestaurant(self, list1, list2):
3+
"""
4+
:type list1: List[str]
5+
:type list2: List[str]
6+
:rtype: List[str]
7+
"""
8+
dict = {}
9+
for i, item in enumerate(list1):
10+
dict[item] = i
11+
12+
minIdxSum = 2 ** 32
13+
res = []
14+
for i, item in enumerate(list2):
15+
if item in dict: #当前餐厅两者都爱
16+
tmp = i + dict[item]
17+
if tmp < minIdxSum: #需要更新最小索引和
18+
res = [item]
19+
minIdxSum = tmp
20+
elif tmp == minIdxSum: #是目前的最小索引和
21+
res.append(item)
22+
return res
23+
24+
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def replaceWords(self, dict, sentence):
3+
"""
4+
:type dict: List[str]
5+
:type sentence: str
6+
:rtype: str
7+
"""
8+
dict = set(dict)
9+
10+
s = sentence.split(" ")
11+
12+
for i, word in enumerate(s):
13+
for j in range(len(word)):
14+
if word[:j + 1] in dict:
15+
s[i] = word[:j + 1]
16+
break
17+
return " ".join(s)
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def numComponents(self, head, G):
9+
"""
10+
:type head: ListNode
11+
:type G: List[int]
12+
:rtype: int
13+
"""
14+
g = []
15+
G = set(G)
16+
p = head
17+
while p: #处理相对排序
18+
if p.val in G:
19+
g.append(p.val)
20+
p = p.next
21+
22+
p, i, res = head, 0, 0
23+
seperate = True #用于标记当前是不是断开
24+
while i < len(g) and p:
25+
if g[i] == p.val:
26+
if seperate == True: #当前是断开状态
27+
res += 1 #所以给res + 1
28+
seperate = False #表示当前是连续
29+
p = p.next
30+
i += 1
31+
else:
32+
seperate = True #已经断开
33+
p = p.next
34+
return res
35+
36+

1119.删去字符串中的元音/1119-删去字符串中的元音.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ def removeVowels(self, S):
44
:type S: str
55
:rtype: str
66
"""
7-
res = ""
8-
for char in S:
9-
if char not in "aeiou":
10-
res += char
11-
12-
return res
7+
return "".join(char for char in S if char not in "aeiou")

1121.将数组分成几个递增序列/1121-将数组分成几个递增序列.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,5 @@ def canDivideIntoSubsequences(self, nums, K):
55
:type K: int
66
:rtype: bool
77
"""
8-
if len(nums) < K:
9-
return False
108
from collections import Counter
11-
d = Counter(nums)
12-
maxx = 1
13-
for key, val in d.items():
14-
maxx = max(val, maxx)
15-
16-
return maxx * K <= len(nums)
9+
return max(Counter(nums).values()) * K <= len(nums)

0 commit comments

Comments
 (0)