class Solution(object):
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
code = ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26']
if s == "" or s[0] == '0':
return 0
dp = [0 for x in range(len(s)+1)]
dp[0] = 1 ...- read more
90. Subsets II
read moreclass Solution(object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() total = [[]] for i in range(1, len(nums) + 1): total += self.subsets_each(nums, i) return total def subsets_each(self, array, target): self.result = [] for i in range(len(array)): self.helper([array[i ...
89. Gray Code
read more# -*- 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 ...
88. Merge Sorted Array
read moreclass 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()
86. Partition 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 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 ...
83. Remove Duplicates from Sorted 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 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 ...
82. Remove Duplicates from Sorted List 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 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 ...
81. Search in Rotated Sorted Array II
read moreclass 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 ...
80. Remove Duplicates from Sorted Array II
read moreclass 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])
79. Word Search
read moreimport 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 ...