zoukankan      html  css  js  c++  java
  • 力扣(LeetCode)--9.回文数

    判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

    示例 1:

    输入: 121
    输出: true

    示例 2:

    输入: -121
    输出: false
    解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

    示例 3:

    输入: 10
    输出: false
    解释: 从右向左读, 为 01 。因此它不是一个回文数。

    class Solution:
        def isPalindrome(self, x: int) -> bool:
            return str(x) == str(x)[::-1]
    

      

    class Solution:
        def isPalindrome(self, x: int) -> bool:
            
            lst = list(str(x))
            L, R = 0, len(lst)-1
            while L <= R:
                if lst[L] != lst[R]:
                    return  False
                L += 1
                R -= 1
            return True
    

      

  • 相关阅读:
    codeforces 1096 题解
    pkuwc 前的任务计划
    codeforces 1093 题解
    luoguP5068 [Ynoi2015]我回来了
    luoguP5074 Eat the Trees
    二分
    保护
    数数字
    旅行
    すすめ!
  • 原文地址:https://www.cnblogs.com/lhy-522/p/14001752.html
Copyright © 2011-2022 走看看