1 (1_twosum)

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

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

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)