leetcode 题号1047 Remove All Adjacent Duplicates In String

查看题目详情可点击此处

题目

Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

We repeatedly make duplicate removals on S until we no longer can.

Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.

Example 1:

Input: "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Note:

1 <= S.length <= 20000
S consists only of English lowercase letters.

解题思路

这题的解题思路与 {% post_link leetcode/stack/easy/leetcode题号844_BackspaceStringCompare Backspace String Compare %} 相似,只不过回退的「#」条件替换为了相同字符回退,当然两个相同的字符是相邻的,每次的比较肯定也是从已读取字符的最后一位开始,可以使用栈的方式解决。

使用栈的方式解决时,对字符串进行遍历读取入栈中,每次读取字符时与栈顶元素比较是否相同,相同就进行出栈操作,直到遍历完字符串。

同样也有原地算法的方式解题,与插入排序相似。将字符数组分成已读取区和未读取区,利用两个指针分别操作两个区域,然后遍历未读取区,将读取到的字符与已读取区末尾字符比较(如果已读取区已经存在字符),字符相同的话已读取区指针减 1,如果不相同将未读取区的当前字符覆盖至已读取区末尾,已读取区指针增 1,直至遍历完字符串。

代码实现

// by stack
public static String removeDuplicates(String S) {
    char[] chars = S.toCharArray();
    Stack<Character> stack = new Stack<>();
    for(int i = 0; i < chars.length; i++) {
        if(stack.empty() || !stack.peek().equals(chars[i])) {
            stack.push(chars[i]);
        } else {
            stack.pop();
        }
    }

    char[] result = new char[stack.size()];
    while(!stack.empty()) {
        result[stack.size() - 1] = stack.pop();
    }

    return new String(result);
}

// by in place
public static String removeDuplicatesInPlace(String S) {
    char[] chars = S.toCharArray();
    int j = 0;
    for(int i = 0; i < chars.length; i++) {
        if(j == 0 || chars[j - 1] != chars[i]) {
            chars[j++] = chars[i];
        } else {
            j--;
        }
    }

    char[] result = new char[j];
    System.arraycopy(chars, 0, result, 0, j);
    return new String(result);
}

代码详情可点击查看 我的 GitHub 仓库

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,906评论 0 38
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 2018年的你, 想成为什么样的人? 想做什么样的事? 过往终究只是过往, 当下比以往更重要。 2017年是特别神...
    发呆烂人阅读 161评论 0 0
  • 她是血统纯正高贵的华夏原产物种,素有“国艳”的美誉,旧时栽在皇家园林中常与玉兰、牡丹、桂花相配植,取“玉棠富贵”之...
    大鸟8wo阅读 1,089评论 10 20
  • 有理论高度才有实践深度 一听说“人生战略”这个词,哇,好有高度呀,太高大上了,想想就不是我等小辈需要去思考的东西,...
    灰色的彩虹阅读 411评论 0 0