-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |