题解 | 吐泡泡

吐泡泡

https://www.nowcoder.com/practice/f86fa2221c094b3d8d1fc79bae450d96

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Stack;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Stack<Character> stack = new Stack<>();
        int T = in.nextInt();
        in.nextLine();
        ArrayList<String> list = new ArrayList<>();
        for(int i = 0; i < T; i++){
            String str = in.nextLine();
            list.add(str);
        }

        for(int i = 0; i < T;i++){
            String temp = list.get(i);
            stack.clear();
            for(Character c : temp.toCharArray()){
            if(stack.isEmpty()){
                stack.push(c);
            }else if(!stack.isEmpty() && stack.peek() == 'o' && c == 'o'){
                stack.pop();
                if(!stack.isEmpty() && stack.peek() == 'O'){
                    stack.pop();
                }else{
                    stack.push('O');
                }
            }else if(!stack.isEmpty() && stack.peek() == 'O' && c == 'O'){
                stack.pop();
            }else{
                stack.push(c);
            }
            }
        for(Character c : stack){
            System.out.print(c);
        }
        System.out.println();
        }
    }
}

全部评论

相关推荐

10-29 19:42
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务