题解 | #点击消除#

点击消除

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

import java.util.Scanner;
import java.lang.String;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str=sc.nextLine();
        char[] c=str.toCharArray();
        Stack<Character> s=new Stack<Character>();
        for(int i=0;i<str.length();i++)
        {
            if(s.isEmpty()||s.peek()!=c[i])
            {
                s.push(c[i]);
            }else{
                s.pop();
            }
        }
        if(s.isEmpty())
        {
            System.out.println("0");
        }else {
            Stack<Character> s1=new Stack<Character>();
            while(!s.isEmpty())
            {
                s1.push(s.pop());
            }
            while(!s1.isEmpty())
            {
                System.out.print(s1.pop());
            }
        }
    }
}

全部评论

相关推荐

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