其他分享
首页 > 其他分享> > 1047. Remove All Adjacent Duplicates In String

1047. Remove All Adjacent Duplicates In String

作者:互联网

这道题很简单,其实是一个Stack的问题, 但是不用Stack,用一个StringBuilder就可以解决,时间复杂度和空间复杂度都是O(n).

    public String removeDuplicates(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (sb.length() > 0 && s.charAt(i) == sb.charAt(sb.length() - 1)) {
                sb.deleteCharAt(sb.length() - 1);
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }

 

标签:1047,String,Duplicates,StringBuilder,复杂度,length,Adjacent,sb,charAt
来源: https://www.cnblogs.com/feiflytech/p/15759207.html