leetcode python 简单难度 解答版本(持续更新)

1 (1_twosum)

这里写图片描述

标准答案1

class Solution(object):
    def twoSum(self, nums, target):
        dic = {}
        for i, num in enumerate(nums):
            if num in dic:
                return [dic[num], i]
            else:
                dic[target - num] = i

2 (7_ReverseInteger)

这里写图片描述

自己的答案:注意有个超出范围溢出!

class Solution(object):
    def reverse(self, x):

        if '-' not in str(x):
            x = [int(i) for i in list(str(x))]
            x = x[::-1]
            x = [str(i) for i in x]
            x = ''.join(x)
            x = int(x)

        else:
            x = str(x).strip('-')
            x = [int(i) for i in list((x))]
            x = x[::-1]
            x = [str(i) for i in x]
            x.insert(0, '-')
            x = ''.join(x)
            x = int(x)

        if (x >-2**31 and x<(2**31-1)):
            return x
        else:
            return 0

标准答案7
经典的除余相结合

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        n = x if x > 0 else -x
        res = 0
        while n:
            res = res * 10 + n % 10
            n = n // 10 #python2的版本需要把这里的//变成/,注意区别 
        if res > 0x7fffffff:
            return 0
return res if x > 0 else -res

3 (9_PalindromeNumber)

这里写图片描述
自己的

class Solution:
    def isPalindrome(selfself, x):
        n = x if x > 0 else -x
        n_ago = n
        res = 0
        while n:
            res = res*10 + n%10
            n = n // 10

        if res > 0x7fffffff:
            return False
        else:
            n_now = res if x > 0 else -res

        if n_ago == n_now:
            return True
        else:
            return False

标准答案

    def isPalindrome(self, x):
        if x < 0:
            return False
        reversed_x = 0
        original_x = x
        while x > 0:
            reversed_x = reversed_x*10 + x%10
            x = x // 10
        return reversed_x == original_x

class Solution_2(object):
    def isPalindrome(self, x):
        return x >= 0 and str(x) == str(x)[::-1]

Input = 12321

A = Solution_2()
B = A.isPalindrome(Input)
print(B)

4 (13_RomanToInteger)

这里写图片描述

这里写图片描述

self

class Solution:
    def romanToInt(self, s):
        symbols_integer = {'I':1, 'V':5, 'X':10, 'L':50,
                           'C':100, 'D':500, 'M':1000,
                           'IV':4, 'IX':9, 'XL':40, 'XC':90,
                           'CD':400, 'CM':900}
        length = len(s)
        integer = 0
        isPass = False

        for i in range(length):
            if isPass:
                isPass = False
                continue

            if s[i] in symbols_integer and s[i:i+2] not in symbols_integer:
                integer = integer + symbols_integer[s[i]]
                isPass = False
                continue

            if s[i:i+2] in symbols_integer:
                integer = integer + symbols_integer[s[i:i+2]]
                isPass = True

        return integer

Input = 'DCXXI'

A = Solution()
B = A.romanToInt(Input)
print(B)

标准

class Solution:
    def romanToInt(self, s):
        map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        num, pre = 0, 1000
        for i in [map[j] for j in s]:
            num, pre = num + i - 2 * pre if i > pre else num + i, i
        return num

Input = 'CDCM'

A = Solution()
B = A.romanToInt(Input)
print(B)

这里写图片描述

17(69_Sqrtx)

这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/minione_2016/article/details/78855124
文章标签: leetcode
个人分类: python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭