leetcode5086

问题描述

给定一个字符串s,其中只包含小写字母。求s的一个子序列t。要求t包含s中的全部字符。如果答案有多个,输出字典序最小的那个子序列。

解法描述

首先,为s中每个字符打标签,表示该字符是否为其类型中的最后一个字符

其次,从左往右扫描s,让每个字符进栈。进栈过程中满足如下约束:

  • 当栈顶元素为其类型中的最后一个字符,此元素不可弹出
  • 当栈顶元素不是其类型最后一个字符且比当前字符大,那么栈顶元素应该被弹出,因为后面还有这类字符,所以先把它弹出去。这是最重要的贪心。
  • 当栈中已经包含当前字符时,如果当前字符isLast=true,那么栈中的对应字符不可弹出。
import java.util.Arrays;

class Solution {
public String smallestSubsequence(String text) {
//初始化isLast
boolean[] isLast = new boolean[text.length()];
boolean[] had = new boolean[26];
for (int i = text.length() - 1; i >= 0; i--) {
int c = text.charAt(i) - 'a';
if (!had[c]) {
had[c] = true;
isLast[i] = true;
} else {
isLast[i] = false;
}
}
int[] sta = new int[text.length()];//定义一个栈,栈中存放的是字符的下标
int si = 0;//定义一个栈顶指针
int[] indexOf = new int[26];//每类字符在栈中的位置
Arrays.fill(indexOf, -1);//-1表示字符不在栈中
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (indexOf[c - 'a'] != -1) {//栈中已经包含了此字符,则使用最新字符更新之,这个地方很关键
sta[indexOf[c - 'a']] = i;
continue;
}
while (si > 0) {//当可以弹栈的时候尽量弹栈
int pos = sta[si - 1];//栈顶元素的下表
if (isLast[pos]) break;//如果栈顶元素是该类字符中的最后一个,那么不能弹
char top = text.charAt(pos);//栈顶字符
if (top < c) break;//如果栈顶字符小于当前字符,停止弹栈
//否则,执行弹栈
indexOf[top - 'a'] = -1;
si--;//弹栈
}
sta[si++] = i;//当前元素入栈
indexOf[c - 'a'] = si - 1;
}
//构造答案
StringBuilder ans = new StringBuilder();
for (int i = 0; i < si; i++) ans.append(text.charAt(sta[i]));
return ans.toString();
}
}

leetcode5086:smallest-subsequence-of-distinct-characters的更多相关文章

  1. [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. LeetCode 1081. Smallest Subsequence of Distinct Characters

    原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...

  3. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  4. [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  5. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  6. Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  7. ✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

  8. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  9. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  10. [Locked] Longest Substring with At Most Two Distinct Characters

    Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...

随机推荐

  1. opencv局限:cv::FileStorage读取中,xml文件的第一层节点不能超过4个

    今天测试发现一个问题,cv::FileStorage读取中,xml文件的第一层节点不能超过4个. <?xml version="1.0"?> <opencv_st ...

  2. mysql版本引起的驱动问题

    mysql版本引起的驱动问题 com.mysql.jdbc.Driver 是 mysql-connector-java 5中的, com.mysql.cj.jdbc.Driver 是 mysql-co ...

  3. 【使用篇二】SpringBoot异常处理(9)

    异常的处理方式有多种: 自定义错误页面 @ExceptionHandler注解 @ControllerAdvice+@ExceptionHandler注解 配置SimpleMappingExcepti ...

  4. 使用构造函数 Boolean 创造的对象不是布尔值,而是对象,typeof new Boolean(1) == 'object'

    注意,使用构造函数 Boolean 创造的对象不是布尔值: 事实上 new Boolean() 返回的是一个 Boolean 对象: typeof new Boolean(1) == 'object' ...

  5. 生鲜超市项目错误及解决办法(安装mysqlclient)

    错误1:安装Mysqlclient D:\Mxshop>pip install mysqlclient- 1.4.4-cp27-cp27m-win32 报错: ERROR: Could not ...

  6. 解决PEnetwork启动的时候提示"An error occured while starting the "TCP/IP Registry Compatibility" Service (2)!"程序将立即退出的问题

    解决PEnetwork启动的时候提示"An error occured while starting the "TCP/IP Registry Compatibility" ...

  7. 8.10 NOIP模拟测试16 Blue+Weed+Drink

    T1 Blue 贪心,每次跳得时候跳能跳到的最远的地方,跳过的就把他设为0,每次二分找到位置,一直跳就行,如果能跳到的位置就是当前位置或比当前位置还小(数组里现在呆着的这一块石头,二分得到的就是当前位 ...

  8. it's over | 2019 CSP-S 第二轮认证(超长预警)

    也许应该从Day -1(2019年11月14日周四)开始说起? 卑微的我们在学长的怂恿下终于...停课了(哇我们太菜了,只停一天半的课有个卵用 早读后我带头去办公室请假,飞哥很大方地答应了,同时免了我 ...

  9. Spring Boot 知识笔记(集成zookeeper)

    一.本机搭建zookeeper伪集群 1.下载安装包,复制三份 2.每个安装包目录下面新建一个data文件夹,用于存放数据目录 3.安装包的conf目录下,修改zoo.cfg配置文件 # The nu ...

  10. oracle--DG查询同步

    查询归档历史: SELECT FIRST_TIME,FIRST_CHANGE#,NEXT_CHANGE#, SEQUENCE# FROM V$LOG_HISTORY; 检查归档文件路径和创建信息 SE ...