leetcode-316.去除重复字母-day8

debug中...

 public String removeDuplicateLetters(String s) {
        //1.去掉重复字母做准备,存储记录字符数量。
        int[] arr = new int[26];
        for (char c : s.toCharArray()) {
            arr[c - 'a']++;
        }
        //StringBuilder ans=new StringBuilder(26);
        Stack<Character> ans = new Stack<>();//用队列也可
        boolean[] inAns = new boolean[26];

        for (char c : s.toCharArray()) {
            arr[c - 'a']--;
            if (ans.isEmpty() || arr[c - 'a'] == 0) {
                ans.push(c);
                inAns[c - 'a'] = true;
            }
            if (inAns[c - 'a']) {
                continue;
            }
            char ch = ans.peek();

            //注意这应该是while
           // 输入:s = "cbacdcbc"
            //输出:"acdb"
            if (c < ch && arr[ch - 'a'] > 0) {
                inAns[ch - 'a'] = false;
                ans.pop();
            }
            ans.push(c);
            inAns[c - 'a'] = true;
        }
        StringBuilder str=new StringBuilder();
        while (!ans.isEmpty()){
            str.append(ans.pop());
        }
        return str.reverse().toString();
    }

 chatgpt修复后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值