1. 38. Count and Say

    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            s="1"
            for i in range(n-1):
                s=self.generator(s)
            return s
    
        def generator(self,nums):
            flag = nums[0]
            count = 1
            newnums = ""
            for i in range(1, len(nums)):
                if nums[i] != flag:
                    newnums += str(count ...
    read more
  2. 36. Valid Sudoku

    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
  3. 35. Search Insert Position

    class 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 ...
    read more
  4. 34. Search for a Range

    class 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 ...
    read more
  5. 31. Next Permutation

    # 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 ...
    read more
  6. Convert pic to txt

    Look at my nyanko sensei picture! かわいい~~

    cat

    # -*- coding: utf-8 -*-
    from PIL import Image
    
    grey2char = ['@', '#', '$', '%', '&', '?', '*', 'o', '/', '{', '[', '(', '|', '!', '^', '~', '-', '_', ':', ';', ',',
                 '.', '`', ' ']
    count = len(grey2char)
    
    
    def toText(image_file):
        image_file = image_file.convert('L')  # 转灰度
        result = ''  # 储存字符串
        for h in range(0, image_file.size[1]):  # height
            for w in range(0, image_file.size[0]):  # width
                gray = image_file.getpixel ...
    read more
  7. 29. Divide Two Integers

    class 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 ...
    read more
  8. 28. Implement strStr()

    class 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 ...
    read more
  9. 27. Remove Element

    class 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 ...
    read more
  10. 26. Remove Duplicates from Sorted Array

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

« Page 14 / 23 »

blogroll

social