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的更多相关文章

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

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

  2. LeetCode 1047. Remove All Adjacent Duplicates In String

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

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

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

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

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

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

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

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

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

  8. How to remove null value in json string

    Hi I'm using the below class Public List<string> name; Public List<string> midname; Once ...

  9. [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

    In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...

随机推荐

  1. 论文阅读笔记五十九:Res2Net: A New Multi-scale Backbone Architecture(CVPR2019)

    论文原址:https://arxiv.org/abs/1904.01169 摘要 视觉任务中多尺寸的特征表示十分重要,作为backbone的CNN的对尺寸表征能力越强,性能提升越大.目前,大多数多尺寸 ...

  2. webapi基于单请求封装多请求的设计【转】

    怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能包含多个子 ...

  3. __str__与__repr__的触发顺序总结

    1.__str__是个内置的方法,无需使用者去调用,其会在满足某一条件时自动触发.那么要触发它运行都有哪些条件呢? 有三种条件,分别为:print , str , %s 2.__repr__同样是个内 ...

  4. Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化

    https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...

  5. MySQL实战45讲学习笔记:第一讲

    一.MySQL逻架构图 二.连接器工作原理刨析 1.连接器工作原理图 2.原理图说明 1.连接命令 mysql -h$ip -P$port -u$user -p 2.查询链接状态 3.长连接端连接 1 ...

  6. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  7. 《30天自制操作系统》笔记3 --- (Day2 上节)完全解析文件系统

    Day2 汇编语言学习与Makefile入门 本文仅带着思路,研究源码里关于文件系统的参数 关于day2主程序部分及更多内容,请看<30天自制操作系统>笔记 导航 发现学习中的变化 源码差 ...

  8. 用 cgroups 管理 cpu 资源

    转自:http://xiezhenye.com/2013/10/用-cgroups-管理-cpu-资源.html 这回说说怎样通过 cgroups 来管理 cpu 资源.先说控制进程的 cpu 使用. ...

  9. ASP.NET MVC 下使用支付宝支付接口 以及 ASP.NET Core 下相关改造支付

    通过nuget首先引用AopSdk.dll 包 下面写的是 Asp.Net MVC 下相关的支付接口 APP支付 配置客户端相关的参数,配置成自己的代码就可以了 private string APPI ...

  10. [MSSQL]找出一天数据中从第一条数据开始每累加1小时的数据

    用Sql Server找出一天数据中从第一条数据开始每累加1小时的数据 -- ============================================= -- Author: Alle ...