# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
queue = [root]
result = []
while queue:
node = queue.pop()
result.append(node ...- read more
143. Reorder List
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reorderList(self, head): """ :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ if not head: return None slow = head fast = head while fast and fast ...
142. Linked List Cycle II
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return slow = head fast = head slow = slow.next fast = fast.next.next while fast ...
141. Linked List Cycle
read more# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head or not head.next: # 0 or 1 item return False slow = head fast = head # >2 item, omit the ...
139. Word Break
read moreclass Solution(object): def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: List[str] :rtype: bool """ if s == '': return True checklist = [False]*(len(s)+1) checklist[len(s)] = True for i in range(len(s)-1,-1,-1): for j in range(i,len(s)): if s[i:j ...
138. Copy List with Random Pointer
read more# -*- coding:utf-8 -*- # Definition for singly-linked list with a random pointer. # class RandomListNode(object): # def __init__(self, x): # self.label = x # self.next = None # self.random = None class Solution(object): def copyRandomList(self, head): """ :type head: RandomListNode :rtype: RandomListNode """ if head == None: return None tmp = head while tmp: # 首先,在原链表的每个节点后面都插入一个新节点,新节点的内容和前面的节点一样 ...
136. Single Number
read moreclass Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ ans = nums[0] for i in range(1, len(nums)): ans = ans ^ nums[i] # x^x=0, x^0=x return ans
134. Gas Station
read moreclass Solution(object): def canCompleteCircuit(self, gas, cost): """ :type gas: List[int] :type cost: List[int] :rtype: int """ sum = 0 # total remained gas subsum = 0 # remained gas for each period index = 0 for i in range(len(gas)): if subsum + gas[i] - cost[i] >= 0: # can come to next station ...
133. Clone Graph
read more# Definition for a undirected graph node # class UndirectedGraphNode: # def __init__(self, x): # self.label = x # self.neighbors = [] class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): return self.clone(node, dict={}) def clone(self, node, dict): if node == None: return None if ...
131. Palindrome Partitioning
read moreclass Solution(object): def partition(self, s): """ :type s: str :rtype: List[List[str]] """ if s == "": return [] result = [] length = len(s) for i in range(len(s)): self.dfs(s[:i + 1], s[i + 1:], length, result, tmp=[]) return result def dfs(self, before, after, length, result, tmp): if self ...