Skip to content

Commit

Permalink
2020-03-06
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Mar 7, 2020
1 parent 73fa4fb commit 4d71f92
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
class Solution(object):
def treeToDoublyList(self, root):
"""
:type root: Node
:rtype: Node
"""
if not root:
return root
if not root.left and not root.right:
root.left = root
root.right = root
return root

left = self.treeToDoublyList(root.left)
right = self.treeToDoublyList(root.right)
if root.left and root.right:
left_tail = left.left
right_tail = right.left

left_tail.right = root
root.left = left_tail
root.right = right
right.left = root

left.left = right_tail
right_tail.right = left
elif root.left:
left_tail = left.left
left_tail.right = root
root.left = left_tail
left.left = root
root.right = left
elif root.right:
right_tail = right.left
root.right = right
root.left = right_tail
right_tail.right = root
right.left = root
return left if left else root
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
# Definition for a Node.
class Node:
def __init__(self, x, next=None, random=None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
mapping = {} # key is the old node, val is the new node

p = head
while p:
mapping[p] = Node(p.val)
p = p.next

p = head
while p:
if p.next:
mapping[p].next = mapping[p.next]
if p.random:
mapping[p].random = mapping[p.random]
p = p.next

return mapping[head] if head else head
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
"""
class Solution(object):
def treeToDoublyList(self, root):
"""
:type root: Node
:rtype: Node
"""
if not root:
return root
if not root.left and not root.right:
root.left = root
root.right = root
return root

left = self.treeToDoublyList(root.left)
right = self.treeToDoublyList(root.right)
if root.left and root.right:
left_tail = left.left
right_tail = right.left

left_tail.right = root
root.left = left_tail
root.right = right
right.left = root

left.left = right_tail
right_tail.right = left
elif root.left:
left_tail = left.left
left_tail.right = root
root.left = left_tail
left.left = root
root.right = left
elif root.right:
right_tail = right.left
root.right = right
root.left = right_tail
right_tail.right = root
right.left = root
return left if left else root
16 changes: 16 additions & 0 deletions 面试题38.字符串的排列/面试题38-字符串的排列.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def permutation(self, s):
"""
:type s: str
:rtype: List[str]
"""
from itertools import permutations
res = []
visited = set()
for item in list(permutations(s)):
s = "".join(item)
if s not in visited:
res.append(s)
visited.add(s)

return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sorted(nums)[len(nums) // 2]
8 changes: 8 additions & 0 deletions 面试题40.最小的k个数/面试题40-最小的k个数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution(object):
def getLeastNumbers(self, arr, k):
"""
:type arr: List[int]
:type k: int
:rtype: List[int]
"""
return sorted(arr)[:k]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
prefix = [0 for _ in nums]

prefix[0] = nums[0]
min_s = min(0, nums[0])
res = nums[0]
for i in range(1, len(nums)):
prefix[i] = prefix[i - 1] + nums[i]
res = max(prefix[i] - min_s, res)
min_s = min(min_s, prefix[i])
return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class MaxQueue(object):

def __init__(self):
from collections import deque
self.queue = deque([])
def max_value(self):
"""
:rtype: int
"""
if self.queue:
return max(self.queue)
return -1

def push_back(self, value):
"""
:type value: int
:rtype: None
"""
self.queue.append(value)


def pop_front(self):
"""
:rtype: int
"""
if not self.queue:
return -1
return self.queue.popleft()


# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()

0 comments on commit 4d71f92

Please sign in to comment.