前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 题目解析之 Palindrome Number

Leetcode 题目解析之 Palindrome Number

原创
作者头像
ruochen
发布2022-01-15 12:19:34
1.3K0
发布2022-01-15 12:19:34
举报
文章被收录于专栏:若尘的技术专栏

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
复制
    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 归档