Palindrome Number && Reverse Number

Problem I: 判断数字是不是回文字符串形式

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.

解决思路

难点在于不使用辅助空间,也就是说不能转成字符串形式再进行判断。

数字操作也就是%和/两种,整体方法如下:

(1)得到最大单位div,如输入的数为1001,则div为1000;

(2)取余和取被除数,然后进行判断,是否相等;

(3)得到去除两边数字的数字,div=div/100,再进入(2),直到输入的数为0为止。

程序

public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }

        int div = 1;
        while (x / div >= 10) {
            div *= 10;
        }

        while (x > 0) {
            int right = x % 10;
            int left = x / div;
            if (left != right) {
                return false;
            }
            x = (x % div) / 10;
            div /= 100;
        }

        return true;
    }
}

Problem II: 翻转数字

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer‘s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

解决思路

善用%和/两种符号。

需要注意的是对于越界情况的处理。

程序

public class Solution {
    public int reverse(int x) {
        long sum = 0;
        while (x != 0) {
            sum *= 10;
            sum += x % 10;
            x /= 10;
        }
        if (sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE) {
            return 0;
        }
        return (int)sum;
    }
}
时间: 2024-08-03 12:16:55

Palindrome Number && Reverse Number的相关文章

HUST 1343 Reverse Number(哈理工 亚洲区选拔赛前练习赛)

G - Reverse Number Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1347 Description Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exc

hust 1347 - Reverse Number

题目描述 Given a non-negative integer sequence A with length N, you can exchange two adjacent numbers each time. After K exchanging operations, what’s the minimum reverse number the sequence can achieve? The reverse number of a sequence is the number of

HDU1266 Reverse Number

问题链接:HDU1266 Reverse Number.基础训练级的题,用C语言编写. 简单的问题,就不说了. AC通过的C语言程序如下: /* HDU1266 Reverse Number */ #include <stdio.h> #include <string.h> #define MAXN 16 int main(void) { int t, start, end; char s[MAXN], c; scanf("%d", &t); while

HDU 1266 Reverse Number(字符串逆转 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM proble

杭电 HDU 1266 Reverse Number

Reverse Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5763    Accepted Submission(s): 2643 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give

LeetCode #Reverse Number#

刚背了单词,然后做个题玩玩-挑个软柿子踩踩-哈哈 很简单的思路.不过好玩的是我忘记检查处理完的数据是否符合整形数据返回了.因而好一会儿不能AC. 感谢 @Fantasy. 很快的指出我没有检查返回数据的范围. 先给出我超丑陋的解(python), 而后给出其他高手给出的很优雅的解!!也是用python 最后会给出利用java和C/C++的解. """ Programmer : EOF Date : 2015.03.31 File : reverse_interger.py &

reverse number

class Solution {public: int reverse(int x) { bool negative_flag=false; if(x==INT_MIN) return 0; if(x<0) { x=-x; negative_flag=true; } long long result=0; while(x!=0) { result=result*10+x%10; x=x/10; } if(result>INT_MAX) return 0; if(negative_flag) r

Leetcode——Reverse number

这个题其实逻辑不难,但是最坑爹的问题是如何判断整型数的溢出!!! class Solution  { public: int reverse(int x) { if (INT_MIN == x) return 0; if (x < 0) return -reverse(-x); int result = 0; while (x > 0){ int a = x % 10; x = x / 10; if ((INT_MAX - a) / 10 < result)    //判断溢出 retu

HDU-1266 Reverse Number

题目链接:点我点我点我 其实这题没啥难度,简单的字符串处理,开学考java练练手而已 只需要注意0 负数 还有 末尾有0的数字就好 另外,书写风格还是太差了. 代码如下: import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.next