【leetcode】1047. Remove All Adjacent Duplicates In String
题目如下:
Given a string
Sof 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 <= 20000Sconsists 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 ...
随机推荐
- p5349 幂
分析 https://www.cnblogs.com/cjyyb/p/10822490.html 代码 #include<bits/stdc++.h> using namespace st ...
- qbzt day6 下午 模拟赛
我太菜了 T2 给定一张有向图,每个点有点权.试找到一条路径,使得该路径上的点权最 大值减去点权最小值最大,问这个差最大是多少. 话说这个题第一个想到的思路是tarjan缩点+拓扑排序来着... ...
- qbzt day5 下午
农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享 ...
- Crypko 基于滚动条进行的动画是如何实现的?
Crypko 网站里面的下拉滚动条进行的动画感觉非常炫,于是研究了一下她的实现,发现她主要是使用了 ScrollMagic 这个库实现了基于滚动条的动画. 为什么这么确定就是用了 ScrollMagi ...
- python 卡方检验例子
python 求拒绝域和卡方值 import scipy.stats as ss obs=[107,198,192,125,132,248] exp=[167]*6 #拒绝域 1%的显著水平,自由度5 ...
- HttpClient实现通过url下载文件
其实就是通过浏览器url,点击就会下载文件. 这里是从代码层面上,对文件进行下载. package main.java.com.abp.util; import org.apache.http.*; ...
- Powershell指令集_1
目录 目录 前言 程序进度条 Write-Progress 执行表达式 Invoke-Expression 表格化打印信息 Format-Table 获取系统服务 Get-WmiObject 循环 F ...
- curl发json
linux 模拟post请求 curl -X POST \ -H "Content-Type: application/json" \ -H "token:GXJP1cl ...
- 在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 ...
- 非GUI 模式运行 jmeter 压力测试
非 GUI 模式,即命令行模式,运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 使用命令:jmeter -n -t <testplan filename> -l <lis ...