leetcode(python)
1.two sum
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
a = nums[i] + nums[j]
if a == target:
return [i,j]
7.Reverse Integer
重做:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x > 0:
x = int(str(x)[::-1])
else:
x = -int(str(-x)[::-1])
if x < 2**31-1 and x >= -2**31:
return x
else:
return 0
(惨不忍睹):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x > 0:
t = x
count = 0
result = 0
a = x
while t >= 1:
t = t//10
count +=1
while count > 0:
result += a%10*pow(10,count-1)
a=a//10
count -=1
if result > 2147483648 or result < -2147483648 :
return 0
else:
return(result)
if x < 0:
t = -x
count = 0
result = 0
a = -x
while t >= 1:
t = t//10
count +=1
while count > 0:
result += a%10*pow(10,count-1)
a=a//10
count -=1
result = -result
if result > 2147483648 or result < -2147483648 :
return 0
else:
return(result)
if x == 0:
return 0
9. Palindrome Number
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
s = str(x)
a = len(s)
b = len(s)//2
if x < 0:
return False
else:
while b:
for i in range(0,a):
if s[i] == s[a-i-1]:
b = b-1
if b == 0:
break
else:
return False
return True
13.罗马数字转数字
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
array1 = [1,5,10,50,100,500,1000]
array2 = ['I','V','X','L','C','D','M']
sum = array1[array2.index(s[len(s)-1])] #s最后一个元素对应的数字
for i in range(len(s)-1):
cur = array1[array2.index(s[len(s)-i-1])] #当前罗马数字转换的数字
num = array1[array2.index(s[len(s)-i-2])] #前一个罗马数字转换的数字
if cur > num:
sum = sum - num
else:
sum = sum + num
return sum
14. Longest Common Prefix
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
# return os.path.commonprefix(strs)
if strs == []:
return ''
else:
for i in range(1, len(strs)):
l1 = len(strs[0])
l2 = len(strs[i])
if l1 > l2:
lenth = l2
else:
lenth = l1
strs[0] = strs[0][0:lenth]
for j in range(lenth):
if strs[0][j] != strs[i][j]:
strs[0] = strs[0][0:j]
break
return strs[0]
20. Valid Parentheses
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
pars = [None]
print(len(pars))
parmap = {')': '(', '}': '{', ']': '['}
for c in s:
if c in parmap and parmap[c] == pars[len(pars)-1]:
pars.pop()
else:
pars.append(c)
return len(pars) == 1
21. Merge Two Sorted Lists
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and not l2: return []
l_result = ListNode(0)
l_merge = l_result
while l1 and l2:
if l1.val <= l2.val:
l_merge.next = l1
l1 = l1.next
else:
l_merge.next = l2
l2 = l2.next
l_merge = l_merge.next
l_merge.next = l1 or l2
return l_result.next
26. Remove Duplicates from Sorted Array
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []:
return 0
else:
k = 0
for i in range(len(nums)-1):
if nums[i] != nums[i+1]:
nums[k] = nums[i]
k+=1
nums[k] = nums[-1]
return k+1
27.删除列表元素
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
length = len(nums)
i = 0
delCount = 0
while i + delCount < length:
if nums[i] == val:
#del(nums[i])
nums.remove(nums[i])
delCount += 1
else:
i += 1
return length-delCount
28. Implement strStr()
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
for i in range(len(haystack)-len(needle)+1):
if haystack[i:i+len(needle)]==needle:
return i
return -1