今天事情比较多,晚上临时刷了几道水题。基本上用一些小技巧就可以AC的,没有什么太大的难度

第三天了

LeetCode #7 反转整数

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x > 0:
            x = Solution.handle_x(x)
        else :
            x = 0 - Solution.handle_x(abs(x))
        return x

    def handle_x(x):
        x = str(x)
        x = x[::-1]
        x = int(x)
        if x > 2**31-1 or x< - 2**31 :
            return 0
        return x

LeetCode #9回文数

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        x = str(x)
        if x == x[::-1]:
            return True
        else :
            return False
        

LeetCode #12整数转罗马数字

class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        r = ''
        romannum ={
            1:'I',
            5:'V',
            10:'X',
            50:'L',
            100:'C',
            500:'D',
            1000:'M'
        }
        if num >= 1000 != 0:
                n = num // 1000
                r = r + Solution.addstr(romannum[1000],n)
                num = num - n*1000
        if num >= 900 : 
                r = r + romannum[100] + romannum[1000]
                num = num - 900
        if num >= 500 :
                r = r + romannum[500]
                num = num - 500
        if num >= 100 :
            n = num // 100
            if n == 4 :
                    r = r + romannum[100] + romannum[500]
            else :
                    r = r + Solution.addstr(romannum[100],n)
            num = num - n * 100
        if num >= 90 :
                r = r + romannum[10] + romannum[100]
                num = num - 90
        if num >= 50 :
                r = r + romannum[50]
                num = num - 50
        if num >= 10 :
            n = num // 10
            if n == 4:
                    r = r + romannum[10] + romannum[50]
            else :
                    r = r + Solution.addstr(romannum[10],n)
            num = num - n * 10
        if num == 9 :
                r = r + romannum[1] + romannum[10]
                num = 0
        if num >= 5  :
                r = r + romannum[5]
                num = num - 5
        if num == 4 :
                r = r + romannum[1] + romannum[5]
                num = 0

        r = r + Solution.addstr(romannum[1],num)
        
        return r

    def addstr(strget,n):
        z = ''
        for i in range(n):
            z += strget
        return z

LeetCode #35 搜索插入位置

class Solution:
    def searchInsert(self, nums, target):
        if nums[-1] < target :
            return len(nums)
        elif nums[0] > target :
            return 0
        else :
            for i in range(len(nums)):
                if nums[i] == target:
                    return i 
                elif nums[i] < target and nums[i+1] >target :
                    return i+1