LeetCode of problem-solving XI: 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:

输入: 121
输出: true

Example 2:

输入: -121
输出: false
解释: 从左向右读,-121 。 从右向左读,121- 。因此它不是一个回文数。

Example 3:

输入: 10
输出: false
解释: 从右向左读,01 。因此它不是一个回文数。

analysis

The main negative analysis to determine not meet the conditions, the other by reversing the string to be judged, as long as the parameters and parameter inversion consistent we believe that this argument is a palindrome can be.

answer

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

        if (x < 10 && x > 0) {
            return true;
        }

        String result = Integer.toString(x);

        char[] chars = result.toCharArray();
        String temp = "";
        for(int i = 0;i < chars.length; i++){
            temp = temp + chars[chars.length-1-i];
        }
        if(temp.equals(result)){
            return true;
        }

        return false;
    }
}
Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/99707953