LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
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);
}
}
参考资料
- https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
- https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)的更多相关文章
- LeetCode.1047-重复删除字符串中的所有相邻重复项
这是小川的第389次更新,第419篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第251题(顺位题号是1047).给定一个小写字母的字符串S,重复删除两个相邻且相等的字母 ...
- LeetCode#1047-Remove All Adjacent Duplicates In String-删除字符串中的所有相邻重复项
一.题目 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们. 在 S 上反复执行重复项删除操作,直到无法继续删除. 在完成所有重复项删除操作后返回最终的字符串.答案 ...
- LeetCode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...
- 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 ...
- 【Leetcode_easy】1047. Remove All Adjacent Duplicates In String
problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- leetCode 题解之字符串中第一个不重复出现的字符
1.题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doe ...
- 【leetcode】1047. Remove All Adjacent Duplicates In String
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- 【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 ...
随机推荐
- C# Base64字符串生成图片
C# Base64字符串生成图片: //签字图片Base64格式去除开头多余字符data:image/png;base64, strSignImg = strSignImg.Substring(str ...
- ICMP隧道
参考文章:http://www.sohu.com/a/297393423_783648
- noip初赛试题
链接: https://pan.baidu.com/s/1yoOMIUqMRBnBUPprC3o6HQ&shfl=shareset 提取码: m8ns 复制这段内容后打开百度网盘手机App,操 ...
- Windows10 Faster R-CNN(GPU版) 配置训练自己的模型
参考链接 1. 找到合适自己的版本,下载安装Anaconda 点击跳转下载安装 Anaconda,双击下载好的 .exe 文件安装,只勾选第一个把 conda 添加到 PATH 路径.
- Lightning Web Components 来自salesforce 的web 组件化解决方案
Lightning Web Components 是一个轻量,快速,企业级别的web 组件化解决方案,官方网站也提供了很全的文档 对于我们学习使用还是很方便的,同时我们也可以方便的学习了解salesf ...
- P5110 【块速递推】
太菜了,不会生成函数,于是用特征方程来写的这道题 首先我们知道,形如\(a_n=A*a_{n-1}+B*a_{n-2}\)的特征方程为\(x^2=A*x+B\) 于是此题的递推式就是:\(x^2=23 ...
- 微信公众号_Deejo说_2019
说明: 1. 文中的内容均来自Deejo说微信公众号 2. 微信中搜索"Deejo说"公众号,可关注 麻麻英语 ——2019.09.10—— It’s my treat. 我来请客 ...
- es6学习1:let和const
一:let 类似var 但是所声明的变量,只在let命令所在的代码块内有效. 1) 不存在变量提升 // var 的情况 console.log(foo); // 输出undefined var ...
- Ubuntu18.04 Server安装Nginx+Git服务和独立的svn服务
安装Nginx+Git 需要安装的包有 nginx, fcgiwrap, git. 其中git在Ubuntu18.04 Server安装时已经默认安装了. 需要安装的是前两个 而fcgiwrap是在 ...
- Python10个图像处理工具
原文地址:https://cloud.tencent.com/developer/article/1498116 译者 | 小韩 来源 | towardsdatascience [磐创AI导读]:本篇 ...