Day 26 - Palindrome Number

大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~


9. Palindrome Number

Question

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.


Example

Example1

Input: x = 121
Output: true

Example2

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example3

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101
Output: false

Constraints

  • -2^31 <= x <= 2^31 - 1

解题

题目

首先先简单的翻译一下题目
给一个数字,判断是不是对称的。

Think

作法大致上是这样

  • 从头跟尾往中间比对,只要有一个是不相同的就回传False

Code

Python

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

Result

  • Python

大家明天见/images/emoticon/emoticon29.gif


<<:  [DAY-27] 适合你的 才是真正的好职涯

>>:  Day27 Javascript元件库 Jquery介绍

Day25 ( 高级 ) 放烟火 1 ( 往上发射 )

放烟火 1 ( 往上发射 ) 教学原文参考:放烟火 1 ( 往上发射 ) 这篇文章会介绍,如何在 S...

NIST SDLC和RMF

安全控制是风险处理的一部分,风险评估之後进行。安全控制的范围是根据风险评估的结果确定的。根据NIST...

[Day 15] 阿嬷都看得懂的开始写第一支 .css 档案罗!

阿嬷都看得懂的开始写第一支 .css 档案罗! 昨天我们介绍了怎麽使用 style 标签,把所有 C...

[Day20]-程序除错与异常处理

程序异常 除数为0的错误 异常处理程序try except 档案不存在的错误FileNotFoun...

Day 11 - [爬虫] 01-蒐集训练资料 以卫服部长照常见问题为例

撰写 Python 程序码蒐集网路上的长照相关问答资讯,相比使用人工蒐集的方式,程序自动化蒐集方便又...