LeetCode【9】Palindrome Number

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.

值得注意的地方就是without extra space。

代码如下:

bool isPalindrome(int x) {
    if(x<0)
        return false;
    for(int i=1;x>=10;i++)
    {
        int first =x,last=x;
        int j=0,k=0;
        last = x%10;
        for(j=0;first>=10;j++)
        {
            first=first/10;
        }
        if(first != last )
            return false;
        for(k=0;k<j;k++)
        {
            first = first*10;
        }
        x = (x-first)/10;
        if(x==0)
            return true;
        first = x;
        for(k=0;first>=10;k++)
            first = first/10;
        j = j-k-2;
        if(j!=0)
        {
            if(x<10)
                return false;
            for(k=0;k<j;k++)
            {
                int tmp=x%10;
                if(tmp!=0)
                    return false;
                x=x/10;
            }
        }
    }
    return true;
}

觉得这样写挺乱的,于是去看别人写的。发现思路一样,每次取首位两个数字,判断是否相同。

给出别人的代码:

bool isPalindrome2(int x) {
    //negative number
    if(x < 0)
        return false;

    int len = 1;
    while(x / len >= 10)
        len *= 10;

    while(x > 0)    {

        //get the head and tail number
        int left = x / len;
        int right = x % 10;

        if(left != right)
            return false;
        else    {
            //remove the head and tail number
            x = (x % len) / 10;
            len /= 100;
        }
    }

    return true;
}

一样的思路,貌似比我的巧妙了不少啊。

时间: 2024-08-05 23:18:41

LeetCode【9】Palindrome Number的相关文章

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number 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 the integer to string, note the restriction of using extra spa

LeetCode【202】Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

【Leetcode-easy】Palindrome Number

思路:除和求余 取得首位和末尾 比较是否相等. 1 public boolean isPalindrome(int x){ 2 if(x<0){ 3 return false; 4 } 5 int div=1; 6 while(x/div>=10){ 7 div*=10; 8 } 9 while(x!=0){ 10 int left=x/div; 11 int right=x%10; 12 if(left!=right){ 13 return false; 14 } 15 x=(x%div)/

【Leetcode长征系列】Single Number II

原题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除

【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<

【CF932G】Palindrome Partition 回文自动机

[CF932G]Palindrome Partition 题意:给你一个字符串s,问你有多少种方式,可以将s分割成k个子串,设k个子串是$x_1x_2...x_k$,满足$x_1=x_k,x_2=x_{k-1}...x_i=x{k-i+1}$. $|s|\le 10^6$ 题解:设字符串的长度为n,考虑字符串$T=s_1s_ns_2s_{n-1}...$.问题就转化成了:求将原串划分成若干个长度为偶数的回文子串的方案数. 首先我们有一种暴力的想法,设f[i]表示将前i个字符分成若干个回文子串的方

【LeetCode】Palindrome Number

Palindrome Number 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 restri

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

【leetcode78】Single Number II

题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra m