[LeetCode] 316. Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example:
Given "bcabc"
Return "abc"
Given "cbacdcbc"
Return "acdb"
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
给定一个只含有小写字母的字符串,移除重复的字符,每种字符只出现1次,返回字符串是按字典顺序中最小的组合。
解法:贪心算法Greedy,用1个HashMap或者数组统计出现的字符。然后再遍历一遍数组, 假设position = 0,每次找到字符比position的小就更新position,同时也更新count,当count[i] == 0的时候,这个字符就是我们要找的结果字符串里的第一个字符。之后因为其他字符的count还都 > 1,继续在s.substring(position + 1)的子串里递归查找第二个字符,注意要在这个子串里把第一个字符清除掉。
Java:
- public class Solution {
- public String removeDuplicateLetters(String s) {
- if(s == null || s.length() == 0) {
- return s;
- }
- int[] count = new int[26];
- char[] res = new char[26];
- int len = s.length();
- for(int i = 0; i < s.length(); i++) {
- count[s.charAt(i) - 'a']++;
- }
- int pos = 0;
- for(int i = 0; i < len; i++) {
- if(s.charAt(i) < s.charAt(pos)) {
- pos = i;
- }
- count[s.charAt(i) - 'a']--;
- if(count[s.charAt(i) - 'a'] == 0) { // found first minimum char
- break;
- }
- }
- String charToRemove = String.valueOf(s.charAt(pos));
- return charToRemove + removeDuplicateLetters(s.substring(pos + 1).replaceAll(charToRemove, ""));
- }
- }
Python:
- class Solution(object):
- def removeDuplicateLetters(self, s):
- """
- :type s: str
- :rtype: str
- """
- remaining = collections.defaultdict(int)
- for c in s:
- remaining[c] += 1
- in_stack, stk = set(), []
- for c in s:
- if c not in in_stack:
- while stk and stk[-1] > c and remaining[stk[-1]]:
- in_stack.remove(stk.pop())
- stk += c
- in_stack.add(c)
- remaining[c] -= 1
- return "".join(stk)
C++:
- class Solution {
- public:
- string removeDuplicateLetters(string s) {
- int m[256] = {0}, visited[256] = {0};
- string res = "0";
- for (auto a : s) ++m[a];
- for (auto a : s) {
- --m[a];
- if (visited[a]) continue;
- while (a < res.back() && m[res.back()]) {
- visited[res.back()] = 0;
- res.pop_back();
- }
- res += a;
- visited[a] = 1;
- }
- return res.substr(1);
- }
- };
All LeetCode Questions List 题目汇总
[LeetCode] 316. Remove Duplicate Letters 移除重复字母的更多相关文章
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
- leetcode 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】316. Remove Duplicate Letters
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
随机推荐
- Mac在zsh环境安装Maven
Mac OS先安装了oh-my-zsh和iterm2,设置系统的默认语言为zsh.再安装Maven的时候,发现添加profile文件,关闭iterm后,mvn的环境变量一直没有生效. 折腾了好久,突然 ...
- NOI2019游记 —— 夏花般绚烂,繁星般璀璨
NOI 2019 游记 夏花般绚烂,繁星般璀璨 打算写成两个形式 Dairy Day -1 早早就到gzez集训了20几天,对整体的环境熟悉很多 在gzez看了场LNR Day 2 然后回到宾馆搞了个 ...
- DATAGRID显示序号
SolidBrush b = new SolidBrush(this.dgvDetail.RowHeadersDefaultCellStyle.ForeColor); e.Gra ...
- GlusterFS集群文件系统研究
https://blog.csdn.net/liuaigui/article/details/6284551 1. GlusterFS概述GlusterFS是Scale-Out存储解决方案G ...
- hibernate的持久化类、主键生成策略
一.hibernate的持久化类 1.什么是持久化类: 持久化:将数据存储到关系型数据库. 持久化类:与数据库中的数据表建立了某种关系的java类.(持久化类=javabean+映射配置文件) 2.持 ...
- openjdk k8s port-forward 连接容器jmx服务
jmx 是java 自带的,如果需要使用我们只需要添加对应的配置即可,以下演示docker 集成jmx 使用kompose 生成k8s 的部署文件,使用port-forward 进行连接,所以java ...
- 一篇来自hasura graphql-engine 百万级别live query 的实践
转自:https://github.com/hasura/graphql-engine/blob/master/architecture/live-queries.md Scaling to 1 mi ...
- ShardingSphere初探1 -- 概览
知道这个框架是通过一期QQ课堂 https://shardingsphere.apache.org 官网 https://github.com/apache/incubator-shardingsph ...
- 金字塔原理(Pyramid Principle)
什么是金字塔原理?简单来说,金字塔原理就是“中心论点---分论点---支撑论据”这样的一个结构. 图片摘自:http://www.woshipm.com/pmd/306704.html 人类通常习惯于 ...
- 使用Sublime Text 写Processing
本来以为是个很简单的事情,没想到一波三折~ 1.下载Sublime Text 3(中文版)并且安装,没啥好说的 2.打开[工具 - 命令面板 - install package],接着就报错了 “Th ...