-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path644.strobogrammatic-number.py
49 lines (45 loc) · 1.08 KB
/
644.strobogrammatic-number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Tag: Hash Table, Simulation
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-246
# Note: -
# A mirror number is a number that looks the same when rotated 180 degrees (looked at upside down).For example, the numbers "69", "88", and "818" are all mirror numbers.
#
# Write a function to determine if a number is mirror.
# The number is represented as a string.
#
# **Example 1:**
# ```
# Input : "69"
# Output : true
# ```
#
# **Example 2:**
# ```
# Input : "68"
# Output : false
# ```
#
#
class Solution:
"""
@param num: a string
@return: true if a number is strobogrammatic or false
"""
def is_strobogrammatic(self, num: str) -> bool:
# write your code here
mirrors = {
'6': '9',
'1': '1',
'8': '8',
'9': '6',
'0': '0'
}
left = 0
right = len(num) - 1
while left <= right:
if num[left] not in mirrors or mirrors[num[left]] != num[right]:
return False
left += 1
right -= 1
return True