当前位置:网站首页>The number of reversing twice in leetcode simple question

The number of reversing twice in leetcode simple question

2022-07-06 15:04:00 ·Starry Sea

subject

reverse An integer means inverting all its bits .

for example , reverse 2021 obtain 1202 . reverse 12300 obtain 321 , Do not retain leading zeros .
Give you an integer num , reverse num obtain reversed1 , Then reverse reversed1 obtain reversed2 . If reversed2 be equal to num , return true ; otherwise , return false .
Example 1:
Input :num = 526
Output :true
explain : reverse num obtain 625 , Then reverse 625 obtain 526 , be equal to num .
Example 2:
Input :num = 1800
Output :false
explain : reverse num obtain 81 , Then reverse 81 obtain 18 , It's not equal to num .
Example 3:
Input :num = 0
Output :true
explain : reverse num obtain 0 , Then reverse 0 obtain 0 , be equal to num .
Tips :
0 <= num <= 10^6
source : Power button (LeetCode)

Their thinking

   As long as a bit is 0 And not a single digit , Then the reversal will not succeed , Otherwise, success .

class Solution:
    def isSameAfterReversals(self, num: int) -> bool:
        return False if str(num)[-1]=='0' and len(str(num))>1 else True

 Insert picture description here

原网站

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131327152855.html