首页 > 其他分享 >回文串判断

回文串判断

时间:2022-11-26 22:55:52浏览次数:45  
标签:判断 StringBuilder 空字符 null true 回文

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;
    }

  

标签:判断,StringBuilder,空字符,null,true,回文
From: https://www.cnblogs.com/Small-sunshine/p/16928563.html

相关文章