题解 | #统计回文#

统计回文

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
           String A = in.nextLine();
           String B = in.nextLine();
           int len = A.length();
           int count = 0;
           for(int i = 0; i <= len; i++) {
            StringBuffer str = new StringBuffer(A);
            str = str.insert(i, B);
             //库函数自带的reverse方法 使用后str就改变了
            StringBuffer tmp = new StringBuffer(str);
            StringBuffer str1 = tmp.reverse();
            //StringBuffer 没有equals方法转字符
            if(str1.toString().equals(str.toString())) {
                count++;
            }
           }
           System.out.println(count);
            
        }
    }
}

全部评论

相关推荐

03-16 22:00
武汉大学 C++
幸福的小熊猫想要offer:我阿里投的 c++岗,面试官说自己是做 java 的,c++这辈子才有了
点赞 评论 收藏
分享
谁知道呢_:要掉小珍珠了,库库学三年,这个结果
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务