Leetcode: Strobogrammatic Number

本文介绍了一个Python函数,用于判断一个字符串形式的数字是否为旋转对称数(即倒置180度后仍与原数相同)。例如,“69”、“88”和“818”都是旋转对称数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is represented as a string.

For example, the numbers “69”, “88”, and “818” are all strobogrammatic.

Hide Tags Hash Table Math
Hide Similar Problems (M) Strobogrammatic Number II (H) Strobogrammatic Number III


My Solution

“`python
class Solution(object):
def isStrobogrammatic(self, num):
“””
:type num: str
:rtype: bool
“”“

    if num==None or num=='':
        return False

    start, end = 0, len(num)-1
    while start<=end:
        if not self.check(num, start, end):
            return False
        start += 1
        end -= 1

    return True

def check(self, num, i, j):
    if i==j:
        if num[i] in ['0','1','8']:
            return True
    else:
        if num[i]==num[j] and num[i] in ['0','1','8']:
            return True
        if (num[i],num[j])==('6','9') or (num[i],num[j])==('9','6'):
            return True

    return False

“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值