1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项)

链接:https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/

题目:

  给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。

  在 S 上反复执行重复项删除操作,直到无法继续删除。

  在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

  示例:

  输入:"abbaca"
  输出:"ca"
  解释:
  例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。

  提示:

  1 <= S.length <= 20000
  S 仅由小写英文字母组成。

思路:

  我这里借助了栈,把字母塞进去,如果下一个和上一个相同就出栈,不然就入栈,最后借助stringbuffer转置一下,生成字符串。内存超过100%,但是时间超过30%,就很烦。

  (idea改变了我的代码规范.......)

代码:

   public static String removeDuplicates(String S) {
char[] ch = S.toCharArray();
Stack stack = new Stack();
int i = 0;
while (i < ch.length) {
if (stack.empty()) {
stack.push(ch[i]);
} else if (stack.peek().equals(ch[i])) {
stack.pop();
} else {
stack.push(ch[i]);
}
i++;
} StringBuilder sb = new StringBuilder();
while (!stack.empty()) {
sb.append(stack.pop());
}
return sb.reverse().toString();
}

LeetCode 1047. Remove All Adjacent Duplicates In String的更多相关文章

  1. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  2. 【Leetcode_easy】1047. Remove All Adjacent Duplicates In String

    problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent ...

  3. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  4. 【leetcode】1047. Remove All Adjacent Duplicates In String

    题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...

  5. LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

  6. 【leetcode】1209. Remove All Adjacent Duplicates in String II

    题目如下: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from ...

  7. 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...

  8. 1047--Remove All Adjacent Duplicates In String

    public class RemoveAllAdjacentDuplicatesInString { /* 解法一:栈 */ public String removeDuplicates(String ...

  9. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

随机推荐

  1. VUE -- 对 Element UI table中数据进行二次处理

    时间——日期 后台经常给我们返回的是 时间戳 (例如:1535620671) 这时候我们页面展现的时候需要将时间戳转换为我们要的格式 例如 (YYYY-MM-DD HH:mm:ss) 如果是在Elem ...

  2. FLYAI

    https://www.flyai.com/d/FacialAge FLYAI  竞赛说明 参加项目竞赛必须实现 model.py 中的predict_all方法,系统才能给出最终分数. 样例代码说明 ...

  3. linux cheese摄像机工具在window电脑上显示

    1.SSH Secure Shell Client 2.Xming 实现步骤: 1.运行Xming工具 2.运行SSH Secure Shell Client,登陆linux系统 输入命令:expor ...

  4. osgViewer::Viewer::Windows

    osg自带窗口去掉边框 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #incl ...

  5. java如何压缩多个文件到压缩包,并下载到浏览器?

    java压缩多个文件到压缩包,并下载到浏览器   解决方法: 完整的方法如下,很简单,亲试有效,极力推荐. 我是以流作为文件,而不是file,循环把所有pdf文件压缩到pdf.zip压缩包中. 1.前 ...

  6. confluent kafka connect remote debugging

    1. Deep inside of kafka-connect start up To begin with, let's take a look at how kafka connect start ...

  7. MyBatis 的案例

    首先我们需要先下载jar包 其次我们书写具体的内容 Student  Class package entity; /* * 学生类 * */ public class Student { //学生编号 ...

  8. linux下无法启动webdriver问题

    linux下无法启动webdriver问题: 查看是否有足够多的webdriver进程: ps -ef | grep chromedriver kill -9 `ps -ef |grepchromed ...

  9. Docker镜像的构建(五)

    目录 构建镜像 1.使用 commit 命令构建 1.1 运行一个要进行修改的容器 1.2 安装 Apache 软件包 1.3 提交定制容器 2.使用 Dockerfile 构建 2.1 我们的第一个 ...

  10. Core JSON及JSON解析

    JSON (JavaScript Object Notation) 是一种基于文档的标准数据交换格式,它可以让应用程序通过网络交换数据.JSON独立于编程语言(Ruby, Java/EE, JavaS ...