1. 89. Gray Code

    # -*- coding: utf-8 -*-
    class Solution(object):
        def grayCode(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
    
            if n == 0:
                return [0]
    
            result = self.grayCode(n - 1)
            seq = list(result)
            for i in reversed(result):
                seq.append((1 << (n - 1)) | i)  # 只要对应的二个二进位有一个为1时,结果位就为1。
    
            return seq
    
    if __name__ == '__main__':
        answer = Solution()
        print answer ...
    read more
  2. 88. Merge Sorted Array

    class Solution(object):
        def merge(self, nums1, m, nums2, n):
            """
            :type nums1: List[int]
            :type m: int
            :type nums2: List[int]
            :type n: int
            :rtype: void Do not return anything, modify nums1 in-place instead.
            """
            for i in range(n):
                nums1[m+i] = nums2[i]
            nums1.sort()
    
    read more
  3. 86. Partition List

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def partition(self, head, x):
            """
            :type head: ListNode
            :type x: int
            :rtype: ListNode
            """
            small = ListNode(0)
            small2 = small
            large = ListNode(0)
            large2 = large
            while head != None:  # create small and large ...
    read more
  4. 83. Remove Duplicates from Sorted List

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head == None:
                return head
            current = head
            previous = None
            while current != None:
                if previous != None and current.val == previous.val:
                    previous ...
    read more
  5. 82. Remove Duplicates from Sorted List II

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head == None:
                return head
            current = head
            previous = None
            prepre = None  # third cursor
            flag = 0
            while current != None:  # test several times ...
    read more
  6. 81. Search in Rotated Sorted Array II

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: bool
            """
            if not nums:
                return False
            start, end = 0, len(nums) - 1
    
            while start <= end:
                mid = (start + end) / 2
                if target == nums[mid]:
                    return True
                while start < mid and nums[start] == nums[mid]:  # tricky ...
    read more
  7. 80. Remove Duplicates from Sorted Array II

    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
    
            i = 0
            for n in nums:
                if i < 2 or n > nums[i - 2]:
                    nums[i] = n
                    i += 1
            return i
    if __name__ == "__main__":
        answer=Solution()
        print answer.removeDuplicates([1,1,1,2,2,3])
    
    read more
  8. 79. Word Search

    import copy
    class Solution(object):
        def exist(self, board, word):
            """
            :type board: List[List[str]]
            :type word: str
            :rtype: bool
            """
            step = [[1, 0], [-1, 0], [0, 1], [0, -1]]
            m = len(board)
            n = len(board[0])
            map = {}
            for i in range(m):
                for j in range(n):
                    if board[i ...
    read more
  9. 78. Subsets

    class Solution(object):
        def subsets(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            total = [[]]
            for i in range(1, len(nums) + 1):
                total += self.subsets_each(nums, i)
            return total
    
        def subsets_each(self, arry, n):
            result = []
            if n == 1:
                for i in range(len(arry)):
                    result.append([arry[i ...
    read more
  10. 77. Combinations

    class Solution(object):
        def combine(self, n, k):
            """
            :type n: int
            :type k: int
            :rtype: List[List[int]]
            """
            self.result = []
            l = list(range(1, n + 1))
            for i in range(n):
                self.createcombine([l[i]],l[i+1:],k)
            return self.result
    
        def createcombine(self, pre, cur, k):
            if len ...
    read more

« Page 9 / 21 »

blogroll

social