Skip to content

Commit 27394bb

Browse files
committed
2019-07-13
1 parent 2fb5d4f commit 27394bb

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def longestConsecutive(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root:
15+
return 0
16+
if not root.left and not root.right:
17+
return 1
18+
19+
self.res = 1
20+
def dfs(node, tmp, parent_val):
21+
if not node:
22+
return
23+
if node.val == parent_val + 1:
24+
tmp += 1
25+
self.res = max(self.res, tmp)
26+
else:
27+
tmp = 1
28+
29+
dfs(node.left, tmp, node.val)
30+
dfs(node.right, tmp, node.val)
31+
32+
dfs(root.left, 1, root.val)
33+
dfs(root.right, 1, root.val)
34+
35+
return self.res
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Logger(object):
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
from collections import defaultdict
8+
self.record = defaultdict(set)
9+
10+
11+
def shouldPrintMessage(self, timestamp, message):
12+
"""
13+
Returns true if the message should be printed in the given timestamp, otherwise returns false.
14+
If this method returns false, the message will not be printed.
15+
The timestamp is in seconds granularity.
16+
:type timestamp: int
17+
:type message: str
18+
:rtype: bool
19+
"""
20+
21+
for i in range(timestamp - 9, timestamp + 1):
22+
if message in self.record[i]:
23+
return False
24+
self.record[timestamp].add(message)
25+
return True
26+
27+
28+
29+
# Your Logger object will be instantiated and called as such:
30+
# obj = Logger()
31+
# param_1 = obj.shouldPrintMessage(timestamp,message)
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 addTwoNumbers(self, l1, l2):
9+
"""
10+
:type l1: ListNode
11+
:type l2: ListNode
12+
:rtype: ListNode
13+
"""
14+
s1, s2 = [], []
15+
while l1:
16+
s1.append(l1.val)
17+
l1 = l1.next
18+
19+
while l2:
20+
s2.append(l2.val)
21+
l2 = l2.next
22+
23+
carry = 0
24+
cur = ListNode(-1)
25+
while s1 or s2:
26+
value = carry
27+
if s1:
28+
value += s1.pop()
29+
if s2:
30+
value += s2.pop()
31+
32+
carry = value > 9
33+
value %= 10
34+
35+
cur.val = value
36+
pre = ListNode(-1)
37+
pre.next = cur
38+
cur = pre
39+
40+
if carry: #´¦Àí¿ÉÄܵĽøÎ»
41+
pre.val = 1
42+
return pre
43+
44+
return pre.next
45+
46+
47+
48+
49+

0 commit comments

Comments
 (0)