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. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  2. .NET中线程同步的几种方法

    lock.Monitor: lock是Monitor的语法糖 [MethodImpl(MethodImplOptions.Synchronized)]: 特性标记一个方法是需要被同步的,最终实现的效果 ...

  3. css网格布局

    先来一段基本布局 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  4. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  5. ros wifi把信号弱的客户端踢出去的脚本

    以下脚本为小于75强度的都踢出去 非capman脚本: /interface wireless registration-table :do {:foreach i in=[find] do={:lo ...

  6. python之lambda、filter、map、reduce的用法说明(基于python2)

    python中有一些非常有趣的函数,面试的时候可能会遇到.今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,m ...

  7. 大话设计模式Python实现-装饰模式

    装饰模式(Decorator Pattern):动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. 下面是一个给人穿衣服的过程,使用装饰模式: #!/usr/bin/en ...

  8. mybatis解决字段名和实体属性不相同

    两种方法: 1.在xml文件里面使用别名 2.使用resultMap标签

  9. CentOS升级Python2.6到Python2.7并安装pip[转载]

    貌似CentOS 6.X系统默认安装的Python都是2.6版本的?平时使用以及很多的库都是要求用到2.7版本或以上,所以新系统要做的第一件事必不可少就是升级Python啦!在这里做个简单的升级操作记 ...

  10. Centos 7搭建Gitlab服务器超详细Centos 7搭建Gitlab服务器超详细(搭建成功)

    一. 安装并配置必要的依赖关系在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问. 注意:用户不 ...