首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Leetcode 题目解析之 Palindrome Number

Leetcode 题目解析之 Palindrome Number

原创
作者头像
ruochen
发布于 2022-01-15 04:19:34
发布于 2022-01-15 04:19:34
1.3K00
代码可运行
举报
运行总次数:0
代码可运行

Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

  1. Could negative integers be palindromes? (ie, -1)
  2. If you are thinking of converting the integer to string, note the restriction of using extra space.
  3. You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
  4. There is a more generic way of solving this problem.
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        if (x >= 0 && x < 10) {
            return true;
        }
        int d = 1;
        while (x / d >= 10) {
            d *= 10;
        }
        while (x != 0) {
            if (x % 10 != x / d) {
                return false;
            }
            x = x % d / 10;
            d /= 100;
        }
        return true;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验