Remove Duplicate Letters I

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once.

Example:

Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "abcd"

 public class Solution {
public String removeDuplicateLetters(String s) {
if (s == null || s.length() <= )
return s; int res = ;
for (int i = ; i < s.length(); i++) {
res = res | ( << s.charAt(i) - 'a');
} StringBuilder sb = new StringBuilder();
int k = ;
for (int i = ; i < ; i++) {
if ((res & (k << i)) != ) {
sb.append((char) ('a' + i));
}
}
return sb.toString();
}
}

Remove Duplicate Letters II

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"

分析:https://segmentfault.com/a/1190000004188227

这道题要保证顺序要一致,而且还是最小的。

读字符的过程中,把字符存到stack里,当发现stack之前存的字符中比当前字符大 (这是一个很好的思路,如果当前字符或者数字需要与前面的字符或者数字比较,并且比较结果和以前的状态不一样,可以考虑用stack。)而且频率还大于0就可以把那个字符pop出去。类似这种题目都可以用stack解决。基本思想就是在一定的限制条件下pop出比当前选择差的元素。

 public class Solution {
public String removeDuplicateLetters(String s) {
int[] freqs = new int[]; // 统计字符频率
for (int i = ; i < s.length(); i++) {
freqs[s.charAt(i)]++;
} boolean[] visited = new boolean[]; // 用来标记存在stack里的字符
Deque<Character> q = new ArrayDeque<>(); for (int i = ; i < s.length(); i++) {
char c = s.charAt(i);
freqs[c]--;
if (visited[c]) continue; // pop出stack当中比当前字符大但后面还存在的的字符,
while (!q.isEmpty() && q.peek() > c && freqs[q.peek()] > ) {
visited[q.pop()] = false;
}
q.push(c);
visited[c] = true;
} StringBuilder sb = new StringBuilder();
for (char c : q) {
sb.append(c);
} return sb.reverse().toString();
}
}

Remove Duplicate Letters I & II的更多相关文章

  1. Remove Duplicate Letters

    316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...

  2. [Swift]LeetCode316. 去除重复字母 | Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  3. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  4. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  5. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

  6. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  7. Remove Duplicate Letters(Java 递归与非递归)

    题目介绍: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  8. 【lintcode】834. Remove Duplicate Letters

    题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  9. 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 ...

随机推荐

  1. 性能工具MiniProfiler在Asp.Net WebForm跟踪EntityFramework

    http://www.xuebuyuan.com/356638.html 选择MiniProfiler.EF 默认会把MiniProfiler安装上,笔者程序是Asp.Net WebForm 不需要安 ...

  2. Scribe日志收集工具

    Scribe日志收集工具 概述 Scribe是facebook开源的日志收集系统,在facebook内部已经得到大量的应用.它能够从各种日志源上收集日志,存储到一个中央存储系统(可以是NFS,分布式文 ...

  3. 整理一下Entity Framework的查询

    整理一下Entity Framework的查询 2012-08-30 13:41:59 标签:Entity Framework 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信 ...

  4. iOS开发工具-网络封包分析工具Charles

    转自唐巧的技术博客:http://blog.devtang.com/blog/2013/12/11/network-tool-charles-intr/ Charles是在Mac下常用的截取网络封包的 ...

  5. Oracle nvl(),nvl2()函数介绍

    NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...

  6. 如何快速有效的修改java的环境变量

    之前已经修改过jdk的环境变量,,,,在/etc/profile下,,, export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera export PATH=${J ...

  7. jupyter notebook + pyspark 环境搭建

    安装并启动jupyter 安装 Anaconda 后, 再安装 jupyter pip install jupyter 设置环境 ipython --ipython-dir= # override t ...

  8. visual studio2010 “类视图”和“对象浏览器”图标

    “类视图”和“对象浏览器”显示一些图标,这些图标表示代码实体,例如命名空间.类.函数和变量. 下表以图文并茂的形式说明了这些图标. 图标 说明 图标 说明 namespace 方法或函数 类 运算符 ...

  9. 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频

    微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...

  10. oracle数据库表空间扩容方法

    1. 先查询表空间在物理磁盘上存放的位置,注意使用sysdba的账号登陆. SELECT tablespace_name, file_id, file_name, ), ) total_space F ...