Leetcode——3 Palindrome Number(回文数)

Posted 真的是从入门开始

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode——3 Palindrome Number(回文数)相关的知识,希望对你有一定的参考价值。

Problem:

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

简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧。

Solution:

public class Solution {
    public boolean isPalindrome(int x) {
        int n=1;
        int copyx=x;
        if(x<0)return false;
        if(x<10)return true;
        while(x>=10){
            x=x/10;
            n++;
            if(x<10)break;
        }
       
        if(n%2==0)
        {
            while(copyx/(int)Math.pow(10,n-1)==copyx%10)
            {
                copyx=copyx%(int)Math.pow(10,n-1);
                copyx=copyx/10;
                // if(x==0)return true;
                n=n-2;
                if(n==0)return true;
                
            }
        }
        if(n%2==1)
        {
            while(copyx/(int)Math.pow(10,n-1)==copyx%10)
            {
                copyx=copyx%(int)Math.pow(10,n-1);
                copyx=copyx/10;
                // if(x==0)return true;
                n=n-2;
                if(n==1)return true;
                
            }
        }
        return false;
    }
}

 

以上是关于Leetcode——3 Palindrome Number(回文数)的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode #9] Palindrome Number

LeetCode 9 Palindrome Number(回文数字判断)

LeetCode 234. Palindrome Linked List

LeetCode 9. Palindrome Number

LeetCode 234 Palindrome Linked List

Leetcode 132. Palindrome Partitioning II