回文串判断

1、直接上代码

 public static boolean isPalindrome(String s) {
        //1、判断字符串是否是null或者是空字符,如果是就返回true
        if (s == null && "".equals(s.trim())) {
            return true;
        }
        //2、移除非字母数字字符,并将大写字母转化为小写字母
        StringBuilder filterStr = new StringBuilder("");
        for (int i = 0; i < s.length(); i++) {
            char currChat = s.charAt(i);
            if (('A' <= currChat && currChat <= 'Z') || ('a' <= currChat && currChat <= 'z') || ('0' <= currChat && currChat <= '9')) {
                filterStr.append(String.valueOf(currChat).toLowerCase());
            }
        }

        //验证字符串是否是回文串
        char [] checkStr = filterStr.toString().toCharArray();
        int minLength = checkStr.length / 2 ;
        for (int j = 0; j < minLength; j++) {
            if (checkStr[j] != checkStr[checkStr.length - 1 - j]) {
                return false;
            }
        }
        return true;
    }

  

posted @ 2022-11-26 22:46  活出自己范儿  Views(18)  Comments(0Edit  收藏  举报