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 ...
随机推荐
- MySQL Error:Warning: (1366, "Incorrect string value: '\\xF0\\x9F\\x98\\x82\\xF0\\x9F...' for column 'xxx' at row 2")
bug现象 使用连接数据库的可视化软件插入 emoj 表情数据.生僻字,可以正常插入.(导致我一直以为跟表情没有任何关系,谷歌出来一堆跟修改数据库.表.字段 的编码的结果....)但是一启动程序插入新 ...
- 注册服务到etcd中
如上存放一些服务的key到etcd中,商品有两个,主要是为了负载均衡的key func NewService() *Service { config := clientv3.Config{ Endpo ...
- UFUN函数 UF_CFI函数(uc4504,uc4540,uc4514,uc4547,UF_CFI_ask_file_exist )
UF_initialize(); //指定本地数据文件的路径 char file_spec[]="D://Program Files//Siemens//NX 8.0//UGII//zyTO ...
- 关于System.ArgumentNullException异常
什么是ArgumentNullException 当将 null 引用(Visual Basic 中为 Nothing)传递到不接受其作为有效参数的方法时引发的异常. 继承 Object Except ...
- 开源项目 02 HttpLib
using JumpKick.HttpLib; using Newtonsoft.Json; using System; using System.Collections.Generic; using ...
- (LIS)最长上升序列(DP+二分优化)
求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...
- centos 7 下安装 redis
一.安装redis服务 第一步:下载redis安装包 命令:wget http://download.redis.io/releases/redis-4.0.6.tar.gz [root@chenzh ...
- Java实现RS485串口通信,发送和接收数据进行解析
最近项目有一个空气检测仪,需要得到空气检测仪的实时数据,保存到数据库当中.根据了解得到,硬件是通过rs485进行串口通讯的,需要发送16进制命令给仪器,然后通过轮询来得到数据. 需要先要下载RXTX的 ...
- RESTFull开发风格
- 剑指offer: 求1+2+...+n
题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路分析: 由于题目的限制条件很多.同样想到 ...