2.回文数

 //2.回文数
    //给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
    //回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

    public boolean isPalindrome(int x){
        //转换
        String num = Integer.toString(x);
        //定义两指针,一个指向头,一个指向尾
        int l =0;
        int r = num.length()-1;
//        循环查找
        while(l<r){
            if(num.charAt(l)!=num.charAt(r)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }

 

posted @ 2022-03-09 20:12  随遇而安==  阅读(26)  评论(0编辑  收藏  举报