首页 > 其他分享 >LeetCode-9 Palindrome Number

LeetCode-9 Palindrome Number

时间:2024-03-27 23:00:55浏览次数:18  
标签:10 Palindrome return reversed Number 121 num str LeetCode

9. Palindrome Number easy

Given an integer x, return true if x is a 

palindrome  

, and false otherwise.

 

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

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.

Example 3:

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

 

Constraints:

  • -231 <= x <= 231 - 1

 

Follow up: Could you solve it without converting the integer to a string?

Solutions:

a. Convert  Integer to String:

class Solution:
    def isPalindrome(self, x: int) -> bool: 
        str_x = str(x)
        len_x = len(str_x)
        for i in range(len_x):
            if str_x[i] != str_x[len_x-i-1]:
                return False
        return True

or

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

b. Dont't Convert  Integer to String:

class Solution:
    # This function checks if a given integer is a palindrome.
    def isPalindrome(self, x: int) -> bool:
        # Check if the number is negative or a multiple of 10 (excluding 0)
        if x < 0 or (x != 0 and x % 10 == 0):
            return False

        reversed_num = 0
        original = x

        # Reverse the second half of the number
        while x > reversed_num:
            reversed_num = reversed_num * 10 + x % 10  # Add the last digit of 'x' to 'reversed_num'
            x //= 10  # Remove the last digit of 'x'

        # Check if the reversed number matches the original number, x == reversed_num // 10  is when X have odd width
        return x == reversed_num or x == reversed_num // 10

 

标签:10,Palindrome,return,reversed,Number,121,num,str,LeetCode
From: https://www.cnblogs.com/millionyh/p/18100520

相关文章

  • LeetCode-21 Merge Two Sorted Lists
    21.MergeTwoSortedLists EasyYouaregiventheheadsoftwosortedlinkedlists list1 and list2.Mergethetwolistsintoone sorted list.Thelistshouldbemadebysplicingtogetherthenodesofthefirsttwolists.Return theheadofthemerg......
  • 【力扣】36. 有效的数独 - 力扣(LeetCode)
    这里主要是记录一些我做这个题遇到的问题及解决办法目录1.问题描述1.1思路分析:2.程序代码及注释:1.问题描述1.1思路分析:  对于个题目而言,我只使用了一个参数,其中我的主要思路就是暴力循环求解,总体思路就是先判断每行是否符合要求,在判断每列是否符合要求、然后......
  • Leetcode 回文链表
    Day12第一题用数组存储链表的数值,在检测是否是回文数组,数组长度不可变,所以用listclassSolution{publicbooleanisPalindrome(ListNodehead){List<Integer>list=newArrayList<>();while(head!=null){list.add(head.val);......
  • leetcode-75
    给定一个包含红色、白色和蓝色、共n个元素的数组nums,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。我们使用整数0、1和2分别表示红色、白色和蓝色。必须在不使用库内置的sort函数的情况下解决这个问题。示例1:输入:nums=[2,0,2,1,1,......
  • elementui组件el-input 类型为number时,去掉上下箭头,并且解决输入中文后光标上移问题
    //去掉number输入框的上下箭头.def-input-numberinput::-webkit-outer-spin-button,.def-input-numberinput::-webkit-inner-spin-button{-webkit-appearance:none;}.def-input-numberinput[type="number"]{-moz-appearance:textfield;}//解决inputnumber框......
  • LeetCodeHot100 链表 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表
    160.相交链表https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-likedpublicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){intlenA=0;intlenB=0;L......
  • leetcode.py
    importrequestsimportbase64importtimedefget_img_res():img=""start_time=time.time()withopen('img1.png','rb')asimage_file:#将图片内容转换为base64编码base64_image=base64.b64encode(image_file.rea......
  • Leetcode 翻转二叉树
    Day11第三题目前ac最快的一道题/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNo......
  • Leetcode 二叉树的最大深度
    Day11第二题深度优先搜索在计算当前二叉树的最大深度时,可以先递归计算出其左子树和右子树的最大深度,然后在\(\mathcal{O}(1)\)时间内计算出当前二叉树的最大深度。classSolution{publicintmaxDepth(TreeNoderoot){if(root==null){retur......
  • Leetcode 和为k的子数组
    Day11第一题#######解题思路:两层循环,用暴力法解决(从不同起始位置开始的子数组)classSolution{publicintsubarraySum(int[]nums,intk){//和为k的子数组的个数计数器countintcount=0;for(intj=0;j<nums.length;j++){......