Title 9, palindrome

Title 9, palindrome

topic

Determine whether an integer is a palindrome. Palindrome correct order (from left to right) and reverse (right to left) reading is the same integer.

Example 1:
Input: 121
Output: true

Example 2:
Input: -121
Output: false
interpretation: read from left to right, -121. Reading from right to left, as 121-. So it is not a palindrome number.

Example 3:
Input: 10
Output: false
interpretation: reading from right to left, to 01. So it is not a palindrome number.

Thinking

This question has several ideas

First:
directly for the questions, obtained by dividing two numbers to be compared, the same once it is determined to continue, directly terminates the loop different, returns false.

Second:
for the category determination palindrome, directly converts the input data into strings, each taking two corresponding characters are the same comparison, the same continues, it returns a different result false.

Code

public class T009 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println( isPalindrome( 23 ) );
	}
	
    public static boolean isPalindrome(int x) {
    	
    	if( x < 0 )
		    return false;
    	else if( x < 10 )
    		return true;
    	else {
    		int l = (x+"").length();

    		while(l>1) {
    			if( x/(int)Math.pow(10, l-1) != x%10 )
    				return false;
    		    x = x%(int)Math.pow(10, l-1);
    		    x = x/10;
    		    l = l-2;
    		}
    		return true;
    	}
        
    }

}
Published 25 original articles · won praise 0 · Views 123

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/103465960