Skip to content

Commit

Permalink
210224做题
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldoer committed Feb 24, 2021
1 parent 28215b8 commit 97a3815
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 008字符串转换整数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re
class Solution:
def myAtoi(self, s: str) -> int:
'''
#re简洁方法
return max(min(int(*re.findall('^[\+\-]?\d+', s.lstrip())), 2**31 - 1), -2**31)
'''#本人方法
s=s.strip()
flag=True
if len(s)==0:
return 0
if s[0]=='-' or s[0]=='+':
if s[0]=='-':
flag=False
s=s[1:]
n=0
for i in s:
if i<='9' and i>='0':
n+=1
else:
break
num=0 if n==0 else int(s[0:n])
if flag==True:
return min(2**31-1,num)
return max(-2**31,-num)

p1=Solution()
print(p1.myAtoi("+-+21k"))


9 changes: 9 additions & 0 deletions 009回文数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def isPalindrome(self, x: int) -> bool:
s=str(x)
for i in range(int(len(s)/2)):
if s[i]!=s[len(s)-i-1]:
return False
return True
p1=Solution()
print(p1.isPalindrome(121))
27 changes: 27 additions & 0 deletions 011盛最多水的容器.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def maxArea(self, height) -> int:
'''#本人超时代码
num=0
h=0
for i in range(len(height)):
for j in range(len(height)-1,i-1,-1):
th=min(height[i],height[j])
if th>h:
tnum=(j-i)*th
if tnum>num:
num=tnum
h=th
return num
'''
#双指针法
i, j, res = 0, len(height) - 1, 0
while i < j:
if height[i] < height[j]:
res = max(res, height[i] * (j - i))
i += 1
else:
res = max(res, height[j] * (j - i))
j -= 1
return res
p1=Solution()
print(p1.maxArea([1,8,6,2,5,4,8,3,7]))

0 comments on commit 97a3815

Please sign in to comment.