代码编织梦想

作者:disappearedgod
时间:2014-8-23

题目

Palindrome Number

  Total Accepted: 19059  Total Submissions: 65626 My Submissions

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

click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

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?

There is a more generic way of solving this problem.


解法

不能有额外的空间(不确定长度的堆空间)
转换成 如何找第一位和最末一位 && 比对后如何处理。
对于int来说 科学计数法:基数、每一位上的值、除法的余数和商


public class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0)
            return false;
        if(x<10)
            return true;
        int base = 1;
        while( x / base >= 10 ){
            base *= 10;
        }

        while(x > 0){// if the length of x[] is odd, base of last cycle is 1
            if(x / base == x % 10){
                x -= base * (x / base);
                x /= 10;
                base /= 100;
            }
            else
                return false;
        }
        return true;
        
    }
}



结果



返回


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/disappearedgod/article/details/38776961

leetcode - palindrome number-爱代码爱编程

Question Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negat

leetcode-palindrome number_handsomechow的博客-爱代码爱编程

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting th

leetcode-palindrome number_lxyer4u的博客-爱代码爱编程

9. Palindrome Number 更多LeetCode答案欢迎大家关注Github: https://github.com/lxyer/LeetCodeAnswer Difficulty: Easy

leetcode-palindrome number(java)_小关学长的博客-爱代码爱编程

1 Description(描述) Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sa

leetcode-palindrome number_田妫的博客-爱代码爱编程

leetcode的easy层次都是一些简单题,一些有递进的简单题。譬如第七题和第九题,方法都是一样,取模整除再相乘,但是第九题有一点不一样的是,第七题已知,取模对正数和负数都一样,但是在第九题的回文数字中,却要求翻转过的数字

leetcode-#9 palindrome number_田园诗人之园的博客-爱代码爱编程

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forw

LeetCode - Palindrome Number-爱代码爱编程

24.07.2021 小白一枚,文章仅当日记本记录学习进度 ;) 如被浏览,请多多指教,非常感谢大家纠错与建议! (因留学顺便练习英语,所以用英文笔记,并无他意)  - Could you solve it without converting the integer to a string? Solution 1 - reverse the

leetcode solution(持续更新,java>c++)_disappearedgod的博客-爱代码爱编程

点击打开链接 作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23621903 时间:2014-4-16 前言 这里是一个刷题汇总的表格,前面刷题一般用的是大家代码的总结,可能注释和版本会多一些。 Title里面分为两

leetcode-twosum_disappearedgod的博客-爱代码爱编程

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23634067 时间:2014-4-13 题目 Two Sum Given an array of integers, find two numbers su

剑指offer 刷题-爱代码爱编程

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/38729255 时间:2014-8-21 前言 首先提供给大家一个刷题的地方: 九度OJ(http://ac.jobdu.com/index.php)不是我