[Leetcode]Remove Duplicate Letters
2016-01-29 12:15
239 查看
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
Return
Given
Return
Example:
Given
"bcabc"
Return
"abc"
Given
"cbacdcbc"
Return
"acdb"
class Solution { public: /*algorithm: greedy idea: 1)based on lexi order,search s, find min char c in S, after c, it should include other chars,we use count[i] to track this 2)S=S.substr(i+1), and remove same char c ,from S 3)repeat the process until S is empty time O(n*k) "bcabc" */ string removeDuplicateLetters(string s) { if(s.size() < 2)return s; string ret; unordered_set<char>cache; vector<int>count(s.size(),0); //build count array for(int i = int(s.size())-1;i >= 0;i--){ cache.insert(s[i]); count[i] = cache.size(); } //select min int mId = 0; for(int i = 1;i < count.size() && count[i] >= count[0];i++){ if(s[mId] > s[i])mId = i; } ret = ret + s[mId]; for(int i = mId+1;i < s.size();){ if(s[i]==s[mId])s.erase(s.begin()+i); else i++; } return ret + removeDuplicateLetters(s.substr(mId+1)); } };
class Solution { public: /*algorithm: stack+greedy idea: 1)one array to track the occurence of each char, one array to track visited or so 2)scan from string begin to end if s[i] < res[last], pop res until res[last] < s[i] push s[i] to res 3) res is the result time O(n) */ string removeDuplicateLetters(string s) { int m[256] = {0}, visited[256] = {0}; char res[27]={'\0'}; int last = -1; for (int i = 0;i < s.size();i++) ++m[s[i]]; for (int i = 0;i < s.size();i++) { --m[s[i]]; if (visited[s[i]]) continue; while (last >= 0 && s[i] < res[last] && m[res[last]]) { visited[res[last]] = 0; last--; } res[++last]=s[i]; visited[s[i]] = 1; } res[last+1]='\0'; return res; } };
相关文章推荐
- 在应用程序中使用 EMOJI 绘文字的方法
- 解决CI调用Ranorex运行web测试失败的问题
- PHP--traits
- 使用css实现并列div高度随动
- IOS 悬浮评论输入框的实现
- final 关键字
- Oracle数据库资源管理
- akka q
- 利用jsPerf优化Web应用的性能
- Unity3D项目之 Survival Shooter 记录
- C#中Abstract和Virtual
- 10016---Redis--简介
- 【NOI2001】食物链
- protobuf语言指南
- UI神器-SOUI
- php面向对象(一),类,类成员,构造方法,析构方法及对象的销毁
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
- C#特性详解
- 【YGOPro编辑器】YGOProDevelop 1.0发布
- Centos下升级Python