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 appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input: "bcabc"
Output: "abc"
Example 2:
Input: "cbacdcbc"
Output: "acdb"
Approach #1: C++. [brute froce]
class Solution {
public:
string removeDuplicateLetters(string s) {
int size = s.size();
if (size == 0) return ""; int l = 0;
string ret = ""; vector<int> cnt(26, 0);
for (int j = 0; j < size; ++j) {
cnt[s[j] - 'a'] = 1;
}
int total_diff = std::accumulate(cnt.begin(), cnt.end(), 0); for (int z = 0; z < total_diff; ++z) {
for (int i = 0; i < 26; ++i) {
int appear = -1;
for (int j = l; j < size; ++j) {
if (s[j] - 'a' == i && ret.find('a' + i) == -1) {
appear = j;
break;
}
}
if (appear == -1) continue; vector<int> cnt2(26, 0);
for (int j = appear; j < size; ++j)
cnt2[s[j] - 'a'] = 1;
for (auto c : ret)
cnt2[c - 'a'] = 1;
int num = std::accumulate(cnt2.begin(), cnt2.end(), 0);
if (num == total_diff) {
ret += char('a' + i);
l = appear + 1;
break;
}
}
} return ret;
}
};
Approach #2: C++.
class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> cand(256, 0);
vector<bool> visited(256, false); for (auto c : s)
cand[c]++; string ret = "0"; for (auto c : s) {
cand[c]--;
if (visited[c]) continue;
while (c < ret.back() && cand[ret.back()]) {
visited[ret.back()] = false;
ret.pop_back();
}
visited[c] = true;
ret += c;
} return ret.substr(1);
}
};
reference:
https://leetcode.com/problems/remove-duplicate-letters/discuss/76767/C%2B%2B-simple-solution-easy-understanding
http://www.cplusplus.com/reference/numeric/accumulate/
316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)的更多相关文章
- 贪心: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 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 316. 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】316. Remove Duplicate Letters
题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- Remove Duplicate Letters
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...
随机推荐
- .Net 框架实现AOP(动态代理实现AOP,本文为翻译)
在上一节,我们将静态实现AOP,但是对于一个大型项目,要想为每个类,每个方法都去实现AOP ,进行日志记录和权限验证似乎是不可能的. 即使可能对于成百上千个类维护,也是很难维护.所以今天的主题就是如标 ...
- JeeSite基础知识(一)
- python's twenty-sixth day for me 模块
configparser 模块: 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键 = 值). 创建文件: # 创建文件 i ...
- leetcode867
vector<vector<int>> transpose(vector<vector<int>>& A) { vector<vector ...
- leetcode594
public class Solution { public int FindLHS(int[] nums) { Dictionary<int, int> dic = new Dictio ...
- Linux 学习笔记之 --- epoll 事件模型详解
epoll 主要采用对已就绪的 fd 进行轮询操作 一.epoll 触发方式 epoll支持 ET 和 LT 两种触发方式 ET(边缘触发):Nginx 就是采用 ET 触发方式,只支持 no-b ...
- 14-EasyNetQ之用Future Publish发布预定中事件
很多商业流程需要事件在未来的时间按照预定时间发布.例如,在初次与客户接触后,可以在未来某个时间去电话回访客户.EasyNetQ可以用它的Future Publish功能帮你实现这个功能.举例:这里我们 ...
- 模拟linux的内存分配与回收
模拟linux的内存分配与回收 要求 通过深入理解内存分配管理的三种算法,定义相应的数据结构,编写具体代码. 充分模拟三种算法的实现过程,并通过对比,分析三种算法的优劣. (1)掌握内存分配FF,BF ...
- Golang简单写文件操作的四种方法
package main import ( "bufio" //缓存IO "fmt" "io" "io/ioutil" ...
- Init & Deinit & ARC
[Init & Deinit] 1.switf中,init不返回值,只负责初始化成员变量.在init方法中可以初始化常量. 2.默认初始化. 3.Swift provides an autom ...