Skip to content

Commit 7bdc2d2

Browse files
authored
Create strobogrammatic_number.py
1 parent 3bca302 commit 7bdc2d2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

strobogrammatic_number.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
@param num: a string
4+
@return: true if a number is strobogrammatic or false
5+
"""
6+
@staticmethod
7+
def check_same(left, right): # 反转180度依然保持一致的数字对
8+
return [left, right] in [['6', '9'], ['9', '6'], ['0', '0'], ['1', '1'], ['8', '8']]
9+
10+
def isStrobogrammatic(self, num):
11+
# write your code here
12+
i, j = 0, len(num) - 1
13+
while i < j:
14+
if Solution.check_same(num[i], num[j]):
15+
i += 1
16+
j -= 1
17+
else:
18+
return False
19+
if i == j: # 反转180度依然保持一致的单个数字
20+
return num[i] in ['0', '1', '8']
21+
else:
22+
return True
23+
24+
# easy: https://www.lintcode.com/problem/strobogrammatic-number

0 commit comments

Comments
 (0)