题解 | #统计回文#

统计回文

http://www.nowcoder.com/practice/9d1559511b3849deaa71b576fa7009dc

import java.util.Scanner;

public class Main {
    public static void main(String[] agrs) {
        Scanner sc = new Scanner(System.in);
        String strA = sc.nextLine();
        String strB = sc.nextLine();
        
        int count = 0;  // 记录插入后符合回文的数量
        for (int i = 0;i <= strA.length(); i++) {
            StringBuilder temp = new StringBuilder(strA);  // 创建一个可变字符类型,用于字符串插入
            temp.insert(i, strB);  // 将strB字符串插入到指定位置
            if (isLM(temp)) {
                count++;
            }
        }
        System.out.print(count);
    }
    
    /*
    传入一个字符串,如果是回文就返回true,不是返回false
    */
    public static boolean isLM(StringBuilder str) {
        for (int i = 0, j = str.length() - 1;i < j; i++, j--) {
            if (str.charAt(i) != str.charAt(j)) {  // 双指针指向判断是否符合
                return false;
            }
        }
        return true;
    }
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务