Palindrome number && Reverse number

Source: Internet
Author: User

Problem I: Judging whether the number is a palindrome string form

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 is thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?

There is a more generic the solving this problem.

Solution Ideas

The difficulty is to not use the auxiliary space, that is, cannot be converted into a string and then judged.

The number operation is also% and/two, the overall method is as follows:

(1) The maximum unit Div is obtained, if the number entered is 1001, then Div is 1000;

(2) Take the remainder and take dividend, then judge, whether equal;

(3) To get rid of the numbers on both sides of the number, div=div/100, and then enter (2) until the number of inputs is 0.

Program

public class Solution {public    boolean ispalindrome (int x) {        if (x < 0) {            return false;        }                int div = 1;        while (X/div >=) {            div *=;        }                while (x > 0) {            int right = x%;            int left = X/div;            if (left! = right) {                return false;            }            x = (x% div)/ten;            div/=;        }                return true;    }}

Problem II: Flipping numbers

Reverse digits of an integer.

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

Click to show spoilers.

Has a thought about this ?

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

If the last digit are 0, what should the output being? ie, cases such as 10, 100.

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

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

Solution Ideas

Make good use of% and/two symbols.

Attention is paid to the handling of cross-border situations.

Program

public class Solution {public    int reverse (int x) {        long sum = 0;        while (x! = 0) {            sum *=;            sum + = x%;            x/=;        }        if (Sum > Integer.max_value | | sum < integer.min_value) {            return 0;        }        return (int) sum;    }}

Palindrome number && Reverse number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.