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 ...
随机推荐
- spring mvc helloworld 和表单功能、页面重定向
Spring MVC Hello World 例子 这里有个很好的教程:https://www.cnblogs.com/wormday/p/8435617.html 下面的例子说明了如何使用 Spri ...
- CentOS yum 安装RabbitMQ
最近在做机器学习的任务系统,任务模块使用了消息对联,比较快速的搭建方法: 1.安装erlang 下载rpm仓库:wget http://packages.erlang-solutions.com/er ...
- Python not readable
try: with open('data.txt','w') as f: for each_line in f: print(each_line)except OSError as reason: p ...
- 前端自动化之ts编译
前端自动化之ts编译 gulp引用包:gulp-tsc gulpfiles.js代码: var typescript = require('gulp-tsc'); gulp.task('compile ...
- struct 方法使用
struct _Worker { _Worker() { pWokerbase=NULL; hThread=INVALID_HANDLE_VALUE; pListConn=NULL; } struct ...
- Zookeeper 基础、工作流、ZAP协议
ZooKeeper 基础 在深入了解ZooKeeper的运作之前,让我们来看看ZooKeeper的基本概念.[1] 我们将在本章中讨论以下主题:1.Architecture(架构)2.Hierarch ...
- 解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx先监听了ipv4的80端口之后又监听了ipv6的80端口,于是就重复占用了.更加坑人的是你去看了端口占用它又把80端口释放了,是不是很囧. 解决方案是编辑nginx的配置文件 修改这一段:
- 【转】LVS负载均衡之session解决方案 持久连接
原文地址:http://minux.blog.51cto.com/8994862/1744761 1. 持久连接是什么? 1.1 在LVS中,持久连接是为了用来保证当来自同一个用户的请求时能够定位到同 ...
- solidity mapping of mapping
solidity mapping of mapping,两层映射,用的时候可以像二维数组一样去访问和修改值,非常方便. 以下代码示例中的这一句: mapping(string => mappin ...
- Oracle——SQL基础
一.SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定义语言DCL: Data ...