1047. 删除字符串中的所有相邻重复项

1047. Remove All Adjacent Duplicates In String

题目描述

LeetCode1047. Remove All Adjacent Duplicates In String简单

Java 实现

import java.util.Stack;

class Solution {
public String removeDuplicates(String S) {
if (S == null || S.length() == 0) {
return S;
}
Stack<Character> stack = new Stack<>();
char[] c = S.toCharArray();
for (int i = 0; i < c.length; i++) {
if (!stack.isEmpty() && c[i] == stack.peek()) {
stack.pop();
} else {
stack.push(c[i]);
}
}
StringBuffer sb = new StringBuffer();
while (!stack.isEmpty()) {
sb.insert(0, stack.pop());
}
return sb.toString();
}
}
class Solution {
public String removeDuplicates(String S) {
int n = S.length();
int i = 0;
char[] c = new char[n];
for (int j = 0; j < n; j++) {
if (i > 0 && c[i - 1] == S.charAt(j)) {
i--;
} else {
c[i++] = S.charAt(j);
}
}
return new String(c, 0, i);
}
}

参考资料

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

  1. LeetCode.1047-重复删除字符串中的所有相邻重复项

    这是小川的第389次更新,第419篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第251题(顺位题号是1047).给定一个小写字母的字符串S,重复删除两个相邻且相等的字母 ...

  2. LeetCode#1047-Remove All Adjacent Duplicates In String-删除字符串中的所有相邻重复项

    一.题目 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们. 在 S 上反复执行重复项删除操作,直到无法继续删除. 在完成所有重复项删除操作后返回最终的字符串.答案 ...

  3. LeetCode 1047. Remove All Adjacent Duplicates In String

    1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...

  4. 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 ...

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

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

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

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

  7. leetCode 题解之字符串中第一个不重复出现的字符

    1.题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doe ...

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

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

  9. 【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 ...

随机推荐

  1. python接口自动化—封装获取常量的类

    背景: 一.执行case的过程: 首先需要,我们能够通过excel获取单元格的内容.获取内容时,首先需要知道获取的数据是哪一行的,这行数据中需要拿那些参数,比如case 名称.请求url.请求方式.h ...

  2. WinDbg常用命令系列---显示引用的内存(dda、ddp、ddu、dpa、dpp、dpu、dqa、dqp、dqu)

    命令dda, ddp, ddu, dpa, dpp, dpu, dqa, dqp, 和 dqu在指定位置显示指针,取消对该指针的引用,然后以各种格式显示结果位置的内存. ddp [Options] [ ...

  3. rust学习

    Rust  (github) 1. install (https://rustup.rs/) 2. play on line curl https://sh.rustup.rs -sSf | sh e ...

  4. gitbook+git+typora 的使用过程

    Typora 下载地址:https://typora.io/ gitbook 第一步:安装 npm install -g gitbook-cli 第二步:使用 对要操作的文件夹执行命令 gitbook ...

  5. Go字符串常用处理

    应用到strings包 /** * @Author: jadeshu * @Description: * @File: main * @Version: 1.0.0 * @Date: 2019/11/ ...

  6. PHP base_convert() 函数

    16进制转8进制 <?php $hex = "E196"; echo base_convert($hex,,); ?> 8进制数转换为10进制数 <?php $o ...

  7. pycharm plot独立窗口显示

    import matplotlib.pyplot as plt ... plt.show() 进行如下设置: File->Settings->Tools->Python Scient ...

  8. Linux系统学习(二)一Linux基本操作

    一.Linux的目录结构 1.1 Linux的目录结构图 1.2 目录内容 /:这就是根目录.对你的电脑来说,有且只有一个根目录.所有的东西,我是说所有的东西都是从这里开始.举个例子:当你在终端里输入 ...

  9. TCP的连接如何知道对方已经异常断开

    断电的话,对方不会发送任何数据包过来,包括RST.主机无法得知.如果是TCP已经连接,有个定时器,会发送空包,sequence number不变.如果一直收不到ack,会断定对方已经无法通信,而释放系 ...

  10. Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor

    Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor 20 ...