1047. 删除字符串中的所有相邻重复项

1047. Remove All Adjacent Duplicates In String

题目描述

LeetCode1047. Remove All Adjacent Duplicates In String简单

Java 实现

import java.util.Stack;

class Solution {
public String removeDuplicates(String S) {
if (S == null || S.length() == 0) {
return S;
}
Stack<Character> stack = new Stack<>();
char[] c = S.toCharArray();
for (int i = 0; i < c.length; i++) {
if (!stack.isEmpty() && c[i] == stack.peek()) {
stack.pop();
} else {
stack.push(c[i]);
}
}
StringBuffer sb = new StringBuffer();
while (!stack.isEmpty()) {
sb.insert(0, stack.pop());
}
return sb.toString();
}
}
class Solution {
public String removeDuplicates(String S) {
int n = S.length();
int i = 0;
char[] c = new char[n];
for (int j = 0; j < n; j++) {
if (i > 0 && c[i - 1] == S.charAt(j)) {
i--;
} else {
c[i++] = S.charAt(j);
}
}
return new String(c, 0, i);
}
}

参考资料

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

  1. LeetCode.1047-重复删除字符串中的所有相邻重复项

    这是小川的第389次更新,第419篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第251题(顺位题号是1047).给定一个小写字母的字符串S,重复删除两个相邻且相等的字母 ...

  2. LeetCode#1047-Remove All Adjacent Duplicates In String-删除字符串中的所有相邻重复项

    一.题目 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们. 在 S 上反复执行重复项删除操作,直到无法继续删除. 在完成所有重复项删除操作后返回最终的字符串.答案 ...

  3. LeetCode 1047. Remove All Adjacent Duplicates In String

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

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

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

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

  6. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

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

  7. leetCode 题解之字符串中第一个不重复出现的字符

    1.题目描述 Given a string, find the first non-repeating character in it and return it's index. If it doe ...

  8. 【leetcode】1047. Remove All Adjacent Duplicates In String

    题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...

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

随机推荐

  1. django-mysql事务

    django文档:https://yiyibooks.cn/xx/django_182/topics/db/transactions.html mysql事务 1) 事务概念 一组mysql语句,要么 ...

  2. vue的操作:使用手机访问电脑的页面做法如下:

    安装好node.js 和 npm 后,配置好路由,且可以在电脑中正常访问页面时, 只要修改我的项目:my-project 下面的config--->index.js--->里面的 host ...

  3. Python 03 pip 的安装和使用

    原文:https://www.runoob.com/w3cnote/python-pip-install-usage.html 原文:https://www.jianshu.com/p/2be68ef ...

  4. 控制论模型&心流模型&波模型

    1.控制论模型 这是对设定的目标,通过多次输入和输出,反馈调节,最终达成目标的方法.广泛运用于自然科学与社会科学中.反馈的周期长短决定了调节精度的大小以及达到目标的速度.反馈结果与目标背离的立即纠正, ...

  5. 【cf contest 1119 H】Triple

    题目 给出 \(n\) 个三元组\(\{ a_i,b_i,c_i \}\)和\(x,y,z\): 将每个三元组扩展成(\(x\)个\(a_i\),\(y\)个\(b_i\),\(z\)个\(c_i\) ...

  6. 帝国CMS万能标签标题截取后自动加入省略号

    帝国CMS万能标签标题截取后自动加入省略号,没有达到字数的则不加省略号完美解决方案1.打开e/class/connect.php  搜索 if(!empty($subtitle))//截取字符  大约 ...

  7. 如何在IDEA上配置Maven

    IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的Java开发工具之一, IDEA是JetBrains公司的产品,现在有逐步取代老牌Java ...

  8. 剑指offer:数组中的逆序对

    题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%100 ...

  9. HearthBuddy投降插件2019-11-01的使用

    在AutoConcede.cs文件中找到如下代码 private List<int> _winList = new List<int> {0, 2, 4, 6, 8}; 现在的 ...

  10. 【Nginx】Nginx服务器配置调优

    1.Nginx服务器配置调优 .设置nginx全局参数 vi /usr/local/nginx/conf/nginx.conf #编辑 worker_processes ; # 工作进程数,为CPU的 ...