1. 142. Linked List Cycle II

    # 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 ...
    read more
  2. 141. Linked List Cycle

    # 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 ...
    read more
  3. 139. Word Break

    class 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 ...
    read more
  4. 138. Copy List with Random Pointer

    # -*- 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:         # 首先,在原链表的每个节点后面都插入一个新节点,新节点的内容和前面的节点一样 ...
    read more
  5. 136. Single Number

    class 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
    
    read more
  6. 134. Gas Station

    class 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 ...
    read more
  7. 133. Clone Graph

    # 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 ...
    read more
  8. 131. Palindrome Partitioning

    class 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 ...
    read more
  9. 130. Surrounded Regions

    class Solution(object):
        def solve(self, board):
            """
            :type board: List[List[str]]
            :rtype: void Do not return anything, modify board in-place instead.
            """
            if not board:
                return
            for i in range(1, len(board) - 1):  # begin from top left 1, bottom right -1
                for j in range(1, len(board[0 ...
    read more
  10. 129. Sum Root to Leaf Numbers

    # 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 sumNumbers(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root == None:
                return 0
            result = []
            self.dfs(root, result, tmp=0)
            return sum(result)
    
        def ...
    read more

« Page 6 / 23 »

blogroll

social