Skip to content

Commit cad5291

Browse files
committedAug 26, 2019
2019-08-25
1 parent 2f12988 commit cad5291

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def invalidTransactions(self, transactions):
3+
"""
4+
:type transactions: List[str]
5+
:rtype: List[str]
6+
"""
7+
recordByName = collections.defaultdict(list)
8+
for trans in transactions:
9+
name, time, amount, city = trans.split(",")
10+
recordByName[name].append([name, int(time), int(amount), city])
11+
12+
def convert(l):
13+
return l[0] + "," + str(l[1]) + "," + str(l[2]) + "," + l[3]
14+
15+
res = set()
16+
for name, rec in recordByName.items():
17+
curRec = sorted(rec, key = lambda x:x[1])
18+
19+
for i in range(len(curRec)):
20+
if curRec[i][2] > 1000:
21+
res.add(convert(curRec[i]))
22+
for j in range(i + 1, len(curRec)):
23+
24+
if abs(curRec[j][1] - curRec[i][1]) > 60:
25+
break
26+
if curRec[j][3] != curRec[i][3]:
27+
res.add(convert(curRec[i]))
28+
res.add(convert(curRec[j]))
29+
return res
30+
31+
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def numSmallerByFrequency(self, queries, words):
3+
"""
4+
:type queries: List[str]
5+
:type words: List[str]
6+
:rtype: List[int]
7+
"""
8+
9+
def func(word):
10+
for char in "abcdefghijklmnopqrstuvwxyz":
11+
if char in word:
12+
return word.count(char)
13+
return 0
14+
15+
def func2(word):
16+
record = collections.Counter(word)
17+
return record[min(record.keys())]
18+
19+
20+
words_count = sorted(map(func2, words))
21+
queries_count = map(func2, queries)
22+
# print words_count, queries_count
23+
ans = []
24+
for query in queries_count:
25+
index = bisect.bisect(words_count, query) #bisect可以迅速找出有index个数 <= query
26+
ans.append(len(words_count) - index)# 减法找有多少个数比query大
27+
return ans
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 removeZeroSumSublists(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
dummy = ListNode(-1)
14+
dummy.next = head
15+
16+
record = {0:dummy}
17+
pre_sum = 0
18+
19+
while head:
20+
pre_sum += head.val
21+
if pre_sum in record:
22+
record[pre_sum].next = head.next
23+
else:
24+
record[pre_sum] = head
25+
head = head.next
26+
return dummy.next

‎1172.餐盘栈/1172-餐盘栈.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from heapq import *
2+
class DinnerPlates(object):
3+
def __init__(self, capacity):
4+
"""
5+
:type capacity: int
6+
"""
7+
self.stack = []
8+
self.c = capacity
9+
self.idx = [] #用于存放有空位的栈的下标
10+
11+
def push(self, val):
12+
"""
13+
:type val: int
14+
:rtype: None
15+
"""
16+
if self.idx:
17+
index = heappop(self.idx) #找最小的空的栈的下标
18+
self.stack[index].append(val) #插入val
19+
if len(self.stack[index]) < self.c: #如果插了之后还没满,就把这个空的栈的下标放进self.idx
20+
heappush(self.idx, index)
21+
else: #现在所有栈都满了,只能新增一个栈在末尾
22+
self.stack.append([val])
23+
if self.c > 1:
24+
self.idx.append(len(self.stack) - 1)
25+
26+
27+
def pop(self):
28+
"""
29+
:rtype: int
30+
"""
31+
while self.stack and not self.stack[-1]:
32+
self.stack.pop()
33+
if not self.stack: #所有的栈都是空的
34+
return -1
35+
else:
36+
if len(self.stack[-1]) == self.c: #如果本来是满的栈
37+
heappush(self.idx, len(self.stack) - 1) #现在要变成有空位的栈了
38+
return self.stack[-1].pop()
39+
40+
def popAtStack(self, index):
41+
"""
42+
:type index: int
43+
:rtype: int
44+
"""
45+
if index >= len(self.stack): #下标越界
46+
return -1
47+
else:
48+
s = self.stack[index] # 把下标为index的栈取出来
49+
if len(s) == self.c: #如果这个栈本来是满的
50+
heappush(self.idx, index) #马上要变空了
51+
return s.pop() if s else -1 #如果这个栈是空的,返回-1, 否则返回pop
52+
53+
54+
# Your DinnerPlates object will be instantiated and called as such:
55+
# obj = DinnerPlates(capacity)
56+
# obj.push(val)
57+
# param_2 = obj.pop()
58+
# param_3 = obj.popAtStack(index)

0 commit comments

Comments
 (0)