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 ...
随机推荐
- Shell编程——test命令
1.整数 如果表达式为真返回值为0,如果表达式为假,返回值为1.test命令可以对整数.字符串.以及文件进行判断. -it:小于 -le:小于或等于 -gt:大于 -ge:大于或等于 -eq:等于 - ...
- 判断所有的input框不能为空
// 判断input框(除了类名为min1和max7)是否为空 function isEmpty(){ var flag=true; $("input[type='text']") ...
- Codeforces Global Round 4 题解
技不如人,肝败吓疯…… 开场差点被 A 题意杀了,幸好仔细再仔细看,终于在第 7 分钟过掉了. 跟榜.wtf 怎么一群人跳题/倒序开题? 立刻紧张,把 BC 迅速切掉,翻到了 100+. 开 D.感觉 ...
- 第09组 Beta冲刺(2/5)
队名:观光队 链接 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 学习 展示GitHub当日代码/文档签入记录 接下来的计划 完成短租车,页面美化 还剩下哪些任务 ...
- Mac:输出phpinfo的信息
输出phpinfo的信息1.echo '<?php phpinfo(); ?>' | php 2>&12.php -a 进入Interactive shell模式然后:pri ...
- oracle--表空间基本操作
--查表空间使用率情况(含临时表空间) SELECT d.tablespace_name "Name", d.status "Status", TO_CHAR ...
- Git恢复之前版本的两种方法reset、revert(图文详解)(转)
一.问题描述在利用github实现多人合作程序开发的过程中,我们有时会出现错误提交的情况,此时我们希望能撤销提交操作,让程序回到提交前的样子,本文总结了两种解决方法:回退(reset).反做(reve ...
- Shell脚本——添加和删除用户
写一个脚本admin_user.sh,其用法格式为: admin_user.sh --add USERLIST --del USERLIST -v|--verbose -h|--help 其中, -h ...
- CountDownLatch 源码分析
CountDownLatch 源码分析: 1:CountDownLatch数据结构 成员变量 Sync类型对象 private final Sync sync; Sync是继承AQS的一个类,Coun ...
- 局域网Linux机器中病毒简单处理 .aliyun.sh 挖矿病毒 ---不彻底
1. 昨天晚上同事打电话给我说自己的服务器上面的redis无故被清空了,并且查看aof 日志有很多 wget和write指令 一想就是大事不好.局域网中病毒了.. 2. 今天早上到公司忙完一阵简单看了 ...