1047--Remove All Adjacent Duplicates In String
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的更多相关文章
- 【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 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
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 【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 ...
- How to remove null value in json string
Hi I'm using the below class Public List<string> name; Public List<string> midname; Once ...
- [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...
随机推荐
- djang项目中的疑问及解决办法(ValueError: Invalid model reference 'apps.user.User'. String model references must be of the form 'app_label.ModelName'.)
这个问题其实就是apps.user.User这种用法是不对的,就在下面的模型中,我本来是绑定apps.user.User,但是试了一下,由于order和user是在同一个apps中,所以直接用user ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- 查看tensorflow是否为MKL版本命令
python -c "import tensorflow; print(tensorflow.pywrap_tensorflow.IsMklEnabled())" source a ...
- pyenv python 多版本管理工具
pyenv fork 自rbenv 以及ruby-build ,然后修改为转为python 使用 venv 以及virtualenv 解决了版本选择的问题,pyenv 同时为我们解决 了python ...
- 关于RAMOS所用的操作系统
关于RAMOS所用的操作系统 RAMOS所用的操作系统,XP就不用说了,精简版最小的600MB到1.5GB的都有.现代意义上的WIN7/8/10 RAMOS一般选用精简版操作系统,节约内存的同时,还能 ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Linux挖矿程序kworkerds分析
0×00 背景概述 近日,同伴的一台Linux服务器中了kworkerds挖矿程序,随即对挖矿程序进行了处理与分析. 0×01服务器现状 进入服务器之后通过top命令,没有发现有占用CPU资源过高的进 ...
- 手写Tomcat服务器
预备知识 编写服务器用到的知识点 1) Socket 编程2) HTML3) HTTP 协议4) 反射5) XML 解析6) 服务器编写 Socket编程 https://www.cnblogs.co ...
- jar解压后重新打包
因为一些原因修改了jar中的配置文件,但用WinRAR压缩成zip文件后该后缀名为jar,发现重新压缩的文件不可用,所有这些情况下我们必须用jar重新打包. 配置Java环境,让jar命令可用: ja ...
- sort和sorted
sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 ...