作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/

题目描述

Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

We repeatedly make duplicate removals on S until we no longer can.

Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.

Example 1:

Input: "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Note:

  1. 1 <= S.length <= 20000
  2. S consists only of English lowercase letters.

题目大意

每次去除字符串中两个相邻且相等的字符,求最后剩下的字符串(结果唯一)。

解题方法

两个相等的字符连续出现则都消除掉,另外题目也说了结果唯一。有点类似与括号匹配问题,所以,我们一个很容易想到栈去解决。

顺序遍历字符串,如果当前的字符和栈顶字符相同,那么出栈;否则,把结果放到栈里面。最后栈里剩余的字符串即为所求。

Python代码如下:

class Solution(object):
def removeDuplicates(self, S):
"""
:type S: str
:rtype: str
"""
stack = []
for s in S:
if stack and s == stack[-1]:
stack.pop()
continue
stack.append(s)
return "".join(stack)

日期

2019 年 5 月 22 日 —— 一个多月不刷题了,重新捡起来

【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)的更多相关文章

  1. LeetCode 1047. Remove All Adjacent Duplicates In String

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

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

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

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

  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】777. Swap Adjacent in LR String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...

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

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

  7. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

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

  9. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

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

随机推荐

  1. 【shell】循环将字符串写入数组中?

    bash shell脚本语法怪异,其他语言循环写入数组或列表都很简单实现,或有相应函数来做. 以下用两种方法来实现: 方法一 c=0 for i in `ls ./Data_Analysis/Quan ...

  2. LATEX公式语法

    see how any formula was written in any question or answer, including this one, right-click on the ex ...

  3. 业务逻辑审批流、审批流、业务、逻辑、面向对象、工作方式【c#】

    ------需求分析:--------1.先按照实际线下流程说这是什么事情,实际要干什么.2.再转换为面向对象-页面的操作流程,演示demo3.再与相关人员沟通是否可行需要什么地方修正.4.最终:线上 ...

  4. sed 修改文件

    总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...

  5. Hive(七)【内置函数】

    目录 一.系统内置函数 1.查看系统自带内置函数 2.查看函数的具体用法 二.常用内置函数 1.数学函数 round 2.字符函数 split concat concat_ws lower,upper ...

  6. Sharding-JDBC 实现水平分库分表

    1.需求分析

  7. 容器之分类与各种测试(四)——unordered_set和unordered_map

    关于set和map的区别前面已经说过,这里仅是用hashtable将其实现,所以不做过多说明,直接看程序 unordered_set #include<stdexcept> #includ ...

  8. 【Java基础】Java中new对象的过程

    序言 联系我上次写的关于Java内存的文章,对象访问在 Java 语言中无处不在,是最普通的程序行为,但即使是最简单的访问,也会却涉及 Java 栈.Java 堆.方法区这三个最重要内存区域之间的关联 ...

  9. Spring 与 SpringBoot 的区别

    概述 Spring 与 SpringBoot 有什么区别???梳理一下 Spring 和 SpringBoot 到底有什么区别,从 Spring 和 SpringBoot 两方面入手. Spring ...

  10. Python 3 字典(Dictionary)

    字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示: d ...