1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 24. Swap Nodes in Pairs

    class 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 ...
    read more
  10. 22. Generate Parentheses

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

« Page 13 / 21 »

blogroll

social