Skip to content

Commit 4d71f92

Browse files
committed
2020-03-06
1 parent 73fa4fb commit 4d71f92

File tree

8 files changed

+206
-0
lines changed

8 files changed

+206
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val, left=None, right=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
"""
9+
class Solution(object):
10+
def treeToDoublyList(self, root):
11+
"""
12+
:type root: Node
13+
:rtype: Node
14+
"""
15+
if not root:
16+
return root
17+
if not root.left and not root.right:
18+
root.left = root
19+
root.right = root
20+
return root
21+
22+
left = self.treeToDoublyList(root.left)
23+
right = self.treeToDoublyList(root.right)
24+
if root.left and root.right:
25+
left_tail = left.left
26+
right_tail = right.left
27+
28+
left_tail.right = root
29+
root.left = left_tail
30+
root.right = right
31+
right.left = root
32+
33+
left.left = right_tail
34+
right_tail.right = left
35+
elif root.left:
36+
left_tail = left.left
37+
left_tail.right = root
38+
root.left = left_tail
39+
left.left = root
40+
root.right = left
41+
elif root.right:
42+
right_tail = right.left
43+
root.right = right
44+
root.left = right_tail
45+
right_tail.right = root
46+
right.left = root
47+
return left if left else root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, x, next=None, random=None):
5+
self.val = int(x)
6+
self.next = next
7+
self.random = random
8+
"""
9+
class Solution(object):
10+
def copyRandomList(self, head):
11+
"""
12+
:type head: Node
13+
:rtype: Node
14+
"""
15+
mapping = {} # key is the old node, val is the new node
16+
17+
p = head
18+
while p:
19+
mapping[p] = Node(p.val)
20+
p = p.next
21+
22+
p = head
23+
while p:
24+
if p.next:
25+
mapping[p].next = mapping[p.next]
26+
if p.random:
27+
mapping[p].random = mapping[p.random]
28+
p = p.next
29+
30+
return mapping[head] if head else head
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val, left=None, right=None):
5+
self.val = val
6+
self.left = left
7+
self.right = right
8+
"""
9+
class Solution(object):
10+
def treeToDoublyList(self, root):
11+
"""
12+
:type root: Node
13+
:rtype: Node
14+
"""
15+
if not root:
16+
return root
17+
if not root.left and not root.right:
18+
root.left = root
19+
root.right = root
20+
return root
21+
22+
left = self.treeToDoublyList(root.left)
23+
right = self.treeToDoublyList(root.right)
24+
if root.left and root.right:
25+
left_tail = left.left
26+
right_tail = right.left
27+
28+
left_tail.right = root
29+
root.left = left_tail
30+
root.right = right
31+
right.left = root
32+
33+
left.left = right_tail
34+
right_tail.right = left
35+
elif root.left:
36+
left_tail = left.left
37+
left_tail.right = root
38+
root.left = left_tail
39+
left.left = root
40+
root.right = left
41+
elif root.right:
42+
right_tail = right.left
43+
root.right = right
44+
root.left = right_tail
45+
right_tail.right = root
46+
right.left = root
47+
return left if left else root
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def permutation(self, s):
3+
"""
4+
:type s: str
5+
:rtype: List[str]
6+
"""
7+
from itertools import permutations
8+
res = []
9+
visited = set()
10+
for item in list(permutations(s)):
11+
s = "".join(item)
12+
if s not in visited:
13+
res.append(s)
14+
visited.add(s)
15+
16+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def majorityElement(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
return sorted(nums)[len(nums) // 2]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def getLeastNumbers(self, arr, k):
3+
"""
4+
:type arr: List[int]
5+
:type k: int
6+
:rtype: List[int]
7+
"""
8+
return sorted(arr)[:k]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def maxSubArray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
prefix = [0 for _ in nums]
8+
9+
prefix[0] = nums[0]
10+
min_s = min(0, nums[0])
11+
res = nums[0]
12+
for i in range(1, len(nums)):
13+
prefix[i] = prefix[i - 1] + nums[i]
14+
res = max(prefix[i] - min_s, res)
15+
min_s = min(min_s, prefix[i])
16+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class MaxQueue(object):
2+
3+
def __init__(self):
4+
from collections import deque
5+
self.queue = deque([])
6+
def max_value(self):
7+
"""
8+
:rtype: int
9+
"""
10+
if self.queue:
11+
return max(self.queue)
12+
return -1
13+
14+
def push_back(self, value):
15+
"""
16+
:type value: int
17+
:rtype: None
18+
"""
19+
self.queue.append(value)
20+
21+
22+
def pop_front(self):
23+
"""
24+
:rtype: int
25+
"""
26+
if not self.queue:
27+
return -1
28+
return self.queue.popleft()
29+
30+
31+
# Your MaxQueue object will be instantiated and called as such:
32+
# obj = MaxQueue()
33+
# param_1 = obj.max_value()
34+
# obj.push_back(value)
35+
# param_3 = obj.pop_front()

0 commit comments

Comments
 (0)