leetcode(3)——回文数, 20210303

题目描述

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

 

解答过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public static boolean isPalindrome(int x) {
        String xstr = String.valueOf(x);
        int i = 0;
        for (i = 0; i < xstr.length() / 2; i++) {
            if (xstr.charAt(i) != xstr.charAt(xstr.length() - 1 - i)) {
                return false;
            }
        }
        if (i >= xstr.length() / 2) {
            return true;
        else {
            return false;
        }
    }
}

 

执行结果:通过

posted @ 2021-03-06 18:39  keepfriend  阅读(40)  评论(0)    收藏  举报