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 ...
随机推荐
- WebSocket实战之JavaScript例子
一.详细代码案例 详细解读一个简单html5 WebSocket的Js实例教程,附带完整的javascript websocket实例源码,以及实例代码效果演示页面,并对本实例的核心代码进行了深入解读 ...
- pcs与crmsh命令比较
一.概念 1.crmsh This project is not part of the GNU Project. Pacemaker command line interface for manag ...
- Pandas:表计算与数据分析
目录 Pandas之Series Pandas之DataFrame 一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的. 3.p ...
- srvctl和crs_start命令无法启动oracle RAC实例, 但sqlplus可以启动
今天遇到一个奇怪问题,发现srvctl和crs_start命令无法启动Oracle RAC实例,但用sqlplus却可以正常启动.最终发现原因是在OCR中数据库的状态变成了disable,将此状态更改 ...
- MySQL5.7 在CentOS 下的安装
尝试了在版本的CentOS6.6 和CentOS7.2 下安装,在6.6下比较复杂些.特地做下记录 在CentOS7.2 下安装,需要在官网下载 mysql-5.7.16-1.el7.x86_64.r ...
- fpga中wire和reg的区别
wire表示直通,即只要输入有变化,输出马上无条件地反映:reg表示一定要有触发,输出才会反映输入.wire表示直通,即只要输入有变化,输出马上无条件地反映:reg表示一定要有触发,输出才会反映输入. ...
- Theos简介
[Theos简介] Theos is a cross-platform suite of development tools for managing, developing, and deployi ...
- T-SQL 理解SQL SERVER中的分区表(转)
转载来源一定要明显: http://www.cnblogs.com/CareySon/archive/2011/12/30/2307766.html 而且这个大神对于数据库方面的文章非常棒 强烈推荐 ...
- ArcGIS JS API实现的距离测量与面积量算
转自https://www.cnblogs.com/deliciousExtra/p/5490937.html
- 217. Contains Duplicate数组重复元素 123
[抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...