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-全文解锁和搜索引擎

    安装和配置 全文检索安装 pip install django-haystack==2.5.1 # 2.7.0只支持django1.11以上版本 搜索引擎安装 pip install whoosh 安 ...

  2. django-签名加密模块It's dangerous--加密token

    https://juejin.im/entry/56b30250df0eea0054375e1d 安装 pip install itsdangerous 使用 from itsdangerous im ...

  3. 使用mybatis框架实现带条件查询-多条件(传入Map集合)

    我们发现我们可以通过传入javaBean的方式实现我们的需求,但是就两个条件,思考:现在就给他传入一个实体类,对系统性能的开销是不是有点大了. 现在改用传入Map集合的方式: 奥!对了,在创建map集 ...

  4. Log4net 数据库存储(四)

    1.新建一个空的ASP.Net 空项目,然后添加Default.aspx窗体 2.添加配置文件:log4net.config <?xml version="1.0" enco ...

  5. Redis-5.0.5集群配置

    版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...

  6. python内置模块2

    十五.shutil模块 ==================================================================== shutil模块是python为我们封 ...

  7. LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力

    二次联通门 : LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 /* LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 叫做计算几 ...

  8. C博客作业02——循环结构

    0.展示PTA总分 单循环题目集 嵌套循环题目集 1.本章学习总结 1.1学习内容总结 (a)while语句 while(表达式) { 循环体语句: } 执行流程:当表达式的值为"真&quo ...

  9. Shiro安全框架-简介

    1. 简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. 实际上,Shiro的主要功能 ...

  10. mysql 存储过程 动态表名

    今天写存储过程时,遇到要将表名最为参数的问题,如果不涉及到游标的话,使用prepare可以解决问题,但是,动态表名要运用在游标中的话,则prepare就得靠边站了. 集众人之智慧,最后,使用临时表解决 ...