1047--Remove All Adjacent Duplicates In String
public class RemoveAllAdjacentDuplicatesInString {
/*
解法一:栈
*/
public String removeDuplicates(String S) {
Stack<Character> stack=new Stack<>();
for (char c:S.toCharArray()){
if (stack.isEmpty()||c!=stack.peek())
stack.push(c);
else
stack.pop();
}
StringBuilder stringBuilder=new StringBuilder();
for (Character character:stack)
stringBuilder.append(character);
return stringBuilder.toString();
}
/*
解法二:StringBuilder模拟栈。
*/
public String removeDuplicates2(String S) {
StringBuilder stringBuilder=new StringBuilder();
int length=0;
for (char c:S.toCharArray()){
if (length!=0&&c==stringBuilder.charAt(length-1))
stringBuilder.deleteCharAt(length-- -1);
else {
stringBuilder.append(c);
length++;
}
}
return stringBuilder.toString();
}
}
1047--Remove All Adjacent Duplicates In String的更多相关文章
- 【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
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】1047. Remove All Adjacent Duplicates In String
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 【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 ...
- How to remove null value in json string
Hi I'm using the below class Public List<string> name; Public List<string> midname; Once ...
- [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...
随机推荐
- requests--会话对象,ssl验证
会话对象 前面我们使用了添加cookie的方式来进行接口的访问,如果有几十个接口都要依赖登录,难道我们都要加上吗? Request的会话对象让你能够跨请求保持某些参数,它也会在同一请求Session实 ...
- 洛谷p3384【模板】树链剖分题解
洛谷p3384 [模板]树链剖分错误记录 首先感谢\(lfd\)在课上调了出来\(Orz\) \(1\).以后少写全局变量 \(2\).线段树递归的时候最好把左右区间一起传 \(3\).写\(dfs\ ...
- CF1194F Crossword Expert(数论,组合数学)
不难的一题.不知道为什么能 $2500$…… 不过场上推错了一直不会优化…… 首先考虑 $f_i$ 表示恰好做完前 $i$ 道题的概率. 这样很难算.修改一下,$f_i$ 表示做完至少 $i$ 道题的 ...
- Linux性能优化实战学习笔记:第四十二讲
一.上节回顾 上一节,我们学习了 NAT 的原理,明白了如何在 Linux 中管理 NAT 规则.先来简单复习一下. NAT 技术能够重写 IP 数据包的源 IP 或目的 IP,所以普遍用来解决公网 ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- C++ 中文乱码的问题
乱码的根本原因就是字符串编码的方式也字符串解码方式不一致导致的, 而在我们平常用的编码编码方式一般都是utf-8以gbk之间的相互转换, 下面给出编码方式的转换代码 string UtfToStrin ...
- Linux下由于注释引发udf编译错误
我们编写如下的UDF: 然后我们放到Linux下进行编译 Linux编译以后会报如下错误: 这是由于C语言的C90标准不支持行注释,而我们只需要将行注释修改为块注释 问题即可解决
- 【沙龙报名中】与微信&云开发官方团队零距离互动,揭秘爆款微信小游戏背后的技术!
有人说 微信小程序游戏的百花齐放 活像十几年前的4399小游戏称霸互联网的景象 " 歪,斗地主吗,三缺二, 不用下app,小程序就能玩,我保证不抢地主让你抢!" ...... &q ...
- 万亿数据下Hadoop的核心竞争力
1.前言 在大数据时代,Hadoop有着得天独厚的优势.然而,每个企业的技术储备和需求特点不同,他们希望从海量的客户数据中挖掘真正的商业价值,像Google.Facebook.Twitter等这样的企 ...
- vulnhub之SP:Harrison靶机
下载地址:‘https://www.vulnhub.com/entry/sp-harrison,302/’ 环境:靶机放在virtualbox上运行,网卡模式 攻击机:kali Linux运行在VMw ...