leetcode5086:smallest-subsequence-of-distinct-characters
问题描述
给定一个字符串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的更多相关文章
- [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- LeetCode 1081. Smallest Subsequence of Distinct Characters
原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- [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 ...
- [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 ...
- 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 ...
- ✡ 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 ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- [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 ...
随机推荐
- 基于Anaconda编译caffe+pycaffe+matcaffe in Ubuntu[不用sudo权限]
目录 caffe 编译 环境 github下载caffe源码 依赖 修改源码的编译配置 报错 测试使用 pycaffe caffe matcaffe caffe 编译 环境 Ubuntu16.04 C ...
- adb命令之解锁打卡
adb devicesadb shell input keyevent 26 按手机电源键adb shell input swipe 400 1080 40 ...
- 【Kafka】Windows环境配置测试
一.配置 1.Java配置:JAVA_HOME路径不要有空格 2.下载/kafka_2.11-1.1.0,地址是https://www.apache.org/dyn/closer.cgi?path=/ ...
- 切换node版本
首先将原来的安装包删了,在控制面板中删除然后在https://nodejs.org/dist/找到想要的版本号 再找到msi文件
- NLP之CRF应用篇(序列标注任务)
1.CRF++的详细解析 完成的是学习和解码的过程:训练即为学习的过程,预测即为解码的过程. 模板的解析: 具体参考hanlp提供的: http://www.hankcs.com/nlp/the-cr ...
- 8.18 NOIP模拟测试25(B) 字符串+乌鸦喝水+所驼门王的宝藏
T1 字符串 卡特兰数 设1为向(1,1)走,0为向(1,-1)走,限制就是不能超过$y=0$这条线,题意转化为从(0,0)出发,走到(n+m,n-m)且不越过$y=0$,然后就裸的卡特兰数,$ans ...
- xunit测试无法找到testhost或没有可用测试的问题解决方法
xunit进行测试,需要安装如下几个包: Microsoft.TestPlatform.TestHost Microsoft.NET.Test.Sdk xunit.runner.visualstudi ...
- C# 使用ConcurrentBag类处理集合线程安全问题
在日常的开发中,经常会遇到多个线程对同一个集合进行读写操作,就难免会出现线程安全问题. 以下代码,如果使用List<T>就会遇到问题:System.InvalidOperationExce ...
- iis7.5和iis8上传文件大小限制和上传时间限制
在IIS 6.0中,不设置默认大小为4M,设置文件上传大小的方法,maxRequestLength(KB),executionTimeout(毫秒),配置如下节点: <system.web> ...
- Python学习笔记系列
1.小甲鱼 python 学习系列笔记