Leetcode:palindrome Number,reverse Integer

Source: Internet
Author: User

determine whether an integer is a palindrome. Do the without extra space.click to show spoilers. Some hints:could negative integers be palindromes? (ie,-1stringif"Reverse Integer"case ?   More generic the solving this problem.

I do this in Java, and it's simple to use a high-level language:

1: Negative returns 0;

2: Convert a number to a character object, use Stringbufer to invert it, and then equals ().

 Public classSolution { Public BooleanIspalindrome (intx) {if(x < 0)return false; String str1=string.valueof (x); StringBuffer SB=NewStringBuffer (STR1); String str2=sb.reverse (). toString (); if(Str1.equals (str2)) {return true; }        Else        {            return false; }    }}

Here's the second one: Reverse Integer

Reverse digits of an integer. Example1:x=123, return321example2:x= -123, Return-321Click to show spoilers. has a thought about this?Here is some good questions to ask before coding. Bonus points forYouifYou have already thought through this!If the integer's last digit are 0, what should the output being? IE, cases such as ten.Did you notice that the reversed integer might overflow? Assume the input is a +-bit Integer, ThenThe reverse of1000000003Overflows. How should handle such cases?for the purpose of this problem, assume that yourfunctionReturns0When the reversed integer overflows. Update ( the- One-Ten): Test cases had been added to Test the overflow behavior.

1: Judging positive and negative numbers

2: Inverted string

3: Construct numbers

 Public intReverseintx) {Longresult = 0; BooleanFlag =false; String Str=string.valueof (x); if(Str.startswith ("-") ) {flag=true; STR= str.substring (1); }        Else if(Str.startswith ("+") ) {flag=false; STR= str.substring (1); } STR=NewStringBuffer (str). reverse (). toString ();  for(inti = 0; I < str.length (); i++) {result= result * + (Str.charat (i)-' 0 '); if(Result >integer.max_value) {                return0; }                    }        if(flag) {result= result * (-1); }        return(int) result; }
The result here is set to a long type, because the int type can overflow and compare with Integer.max_value.

Leetcode:palindrome Number,reverse Integer

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.