Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit310929e

Browse files
committed
Update to 016
Update to 016
1 parentfa61f11 commit310929e

8 files changed

+169
-0
lines changed

‎Python3/007_Reverse_Integer.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Reverse digits of an integer.
5+
6+
Example1: x = 123, return 321
7+
Example2: x = -123, return -321
8+
9+
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
10+
11+
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
12+
13+
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
14+
15+
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
16+
'''
17+
18+
19+
classSolution(object):
20+
defreverse(self,x):
21+
"""
22+
:type x: int
23+
:rtype: int
24+
"""
25+
# Consider positive and negative situation
26+
flag=0
27+
ifx<0:
28+
flag=-1
29+
ifx>0:
30+
flag=1
31+
x*=flag
32+
result=0
33+
whilex:
34+
result=result*10+x%10
35+
x//=10
36+
ifresult>2**32-1:#2的32次方
37+
return0
38+
else:
39+
returnresult*flag
40+
41+
42+
if__name__=="__main__":
43+
assertSolution().reverse(654300)==3456
44+
assertSolution().reverse(-321)==-123
45+
assertSolution().reverse(21474836470)==0
46+
47+
48+
49+
50+
51+
52+
53+

‎Python3/009_Palindrome_Number.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
'''
6+
Determine whether an integer is a palindrome. Do this without extra space.
7+
8+
Could negative integers be palindromes? (ie, -1)
9+
10+
If you are thinking of converting the integer to string, note the restriction of using extra space.
11+
12+
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
13+
14+
There is a more generic way of solving this problem.
15+
'''
16+
17+
classSolution(object):
18+
defisPalindrome(self,x):
19+
"""
20+
:type x: int
21+
:rtype: bool
22+
"""
23+
ifx<0:
24+
returnFalse
25+
div=1
26+
whilex/div>=10:
27+
div*=10
28+
whilex>0:
29+
l=x//div
30+
r=x%10
31+
32+
ifl!=r:
33+
returnFalse
34+
x%=div
35+
x//=10
36+
37+
div/=100
38+
returnTrue
39+
40+
41+
42+
43+
if__name__=="__main__":
44+
assertSolution().isPalindrome(123)==False
45+
assertSolution().isPalindrome(12321)==True
46+
assertSolution().isPalindrome(-121)==False
47+
48+
49+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
classSolution(object):
6+
defmaxArea(self,height):
7+
"""
8+
:type height: List[int]
9+
:rtype: int
10+
"""
11+
ifnotheight:
12+
return0
13+
left=0
14+
right=len(height)-1
15+
result=0
16+
whileleft<right:
17+
ifheight[left]<height[right]:
18+
area=height[left]* (right-left)
19+
result=max(result,area)
20+
left+=1
21+
else:
22+
area=height[right]* (right-left)
23+
result=max(result,area)
24+
right-=1
25+
returnresult
26+
27+
28+
if__name__=="__main__":
29+
assertSolution().maxArea([1,1])==1
30+
31+
32+

‎Python3/012_Integer_to_Roman.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
6+
7+

‎Python3/013_Roman_to_Integer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
6+
7+

‎Python3/014_Longest_Common_Prefix.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
6+
7+

‎Python3/015_3Sum.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
6+
7+

‎Python3/016_3Sum_Closest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
5+
6+
7+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp