class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in range(9):
for j in range(9):
if board[i][j]!="." and not self.checkSudoku(i,j,board):
return False
return True
def checkSudoku(self,i,j,board):
for compare in range(9 ...- read more
35. Search Insert Position
read moreclass Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ if target <=nums[0]: return 0 else: for i in range(len(nums)): if target>nums[i]: continue else: return i return len(nums) if __name__ == "__main__": answer=Solution() print answer.searchInsert([1 ...
34. Search for a Range
read moreclass Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ if len(nums) == 0: return [-1, -1] left = self.findtheleft(nums, target) right = self.findtheright(nums, target) return [left, right] def findtheleft(self, nums, target): left = 0 right = len(nums) - 1 if ...
31. Next Permutation
read more# 1. From right to left, find the first digit (PartitionNumber) which violate the increase trend. # 2. From right to left, find the first digit which larger than PartitionNumber, call it ChangeNumber. # 3. Swap the PartitionNumber and ChangeNumber. # 4. Reverse all the digit on the right of partition index. class Solution ...
29. Divide Two Integers
read moreclass Solution(object): def divide(self, dividend, divisor): """ :type dividend: int :type divisor: int :rtype: int """ INT_MAX=(1<<31)-1 if divisor==0: return INT_MAX else: symbol = dividend > 0 and divisor < 0 or dividend < 0 and divisor > 0 count=0 shift=31 dividend,divisor=abs(dividend),abs(divisor) while shift ...
28. Implement strStr()
read moreclass Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if not needle: return 0 for i in range(len(haystack) - len(needle) + 1): if haystack[i] == needle[0]: j = 1 while j < len(needle) and haystack[i + j] == needle[j]: j += 1 if ...
27. Remove Element
read moreclass Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ j=len(nums)-1 for i in range(len(nums)-1,-1,-1): if nums[i]==val: nums[i],nums[j]=nums[j],[i] j-=1 return j+1 if __name__ == "__main__": answer ...
26. Remove Duplicates from Sorted Array
read moreclass Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if nums == []: return 0 count = 0 for i in range(0, len(nums)): if i and nums[i] == nums[i - 1]: continue else: nums[count] = nums[i] count += 1 return count if __name__ == "__main__": answer=Solution() print ...
24. Swap Nodes in Pairs
read moreclass Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ if head == None or head.next == None: return head dummy = ListNode(0); dummy.next = head p = dummy while p.next and p.next.next: tmp = p.next.next p.next.next ...
22. Generate Parentheses
read moreclass Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ mylist=[] if n == 0: return [] else: if n == 1: return ["()"] else: for item in self.generateParenthesis(n-1): for i in range(0,(n-1)*2): newp="("+item[:i]+")"+item[i:] if newp not in mylist: mylist ...