题解 | 点击消除

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

import java.util.Scanner;
import java.util.Stack;
public class Main {
    public static void main(String[] args){
        Scanner sc  = new Scanner(System.in);
        String s = sc.next();
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<s.length();i++){
            if(!stack.isEmpty()&&s.charAt(i)==stack.peek()){
                stack.pop();
                continue;
            }
            stack.push(s.charAt(i));
        }
        if(stack.isEmpty()){
            System.out.print(0);
        }else{
            Stack<Character> stack2 = new Stack<>();
            while(!stack.isEmpty()){
                stack2.push(stack.pop());

            }
            while(!stack2.isEmpty()){
                System.out.print(stack2.pop());
            }
        }
    }
}

模仿评论区的写的

全部评论

相关推荐

醉蟀:你不干有的是人干
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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