-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |