【leetcode】1047. 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 <= S.length <= 20000
S
consists only of English lowercase letters.
解题思路:注意两个相同的元素相邻就能消掉,三个相同的元素相邻还会剩下一个。解法也很简单,遍历Input,如果当前字符和前一个字符一样,则两者都消除。
代码如下:
class Solution(object):
def removeDuplicates(self, S):
"""
:type S: str
:rtype: str
"""
stack = []
for i in S:
if len(stack) == 0 or i != stack[-1]:
stack.append(i)
else:
del stack[-1]
return ''.join(stack)
【leetcode】1047. Remove All Adjacent Duplicates In String的更多相关文章
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【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】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 ...
- 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)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【leetcode】1233. Remove Sub-Folders from the Filesystem
题目如下: Given a list of folders, remove all sub-folders in those folders and return in any order the f ...
随机推荐
- 【洛谷P1310 表达式的值】
题目链接 题目描述 对于1 位二进制变量定义两种运算: 运算的优先级是: 先计算括号内的,再计算括号外的. “× ”运算优先于“⊕”运算,即计算表达式时,先计算× 运算,再计算⊕运算.例如:计算表达式 ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_1_HashSet集合介绍
特点:不允许有重复的记录,无序的集合 set不允许重复.接口中没有索引.所以方法和Collection中的方法是一样的,没有带索引的方法 因为Set的方法和Collection都是一样的.所以这里不再 ...
- Leaflet
https://leafletjs.com/ https://github.com/Leaflet/Leaflet
- 在centos7.4 nginx mysql php部署 thinkphp5.0 项目
系统 centos7 环境 php 7.1.3 nignx 1.12.2 mysql 5.5.6 我是通过lnmp 集成环境安装 fastcgi.conf 末尾添加 vim fastcig.conf ...
- html php插入百度地图定位
CSS样式 1 2 3 4 <style> .iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hi ...
- python 正则表达式 re.search
#coding:utf-8 import re #将正则表达式编译为pattern对象 #compile(pattern, flags=0) #Compile a regular expression ...
- mybatis初步理解
mybatis概念 mybatis 是一款轻量级的orm的数据持久框架,封装jdbc 对开发提供了便利,但是性能会比jdbc低,从开发的角度来说,现在是比较流行的 掌握上比较容易,也支持缓存,级联 ...
- python+selenium的frame表单切换
switch_to.frame() 切换frame switch_to.default_content() 切 ...
- Java日志使用slf4j 配置log4j后,有日志文件 但日志文件内容为空
SLF4J的全称是Simple Logging Facade for Java,即简单日志门面. SLF4J并不是具体的日志框架,而是作为一个简单门面服务于各类日志框架,如java.util.logg ...