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 ...
随机推荐
- [转]MVC & JavaEE三层架构
- React中ref的三种用法 可以用来获取表单中的值 这一种类似document.getXXId的方式
import React, { Component } from "react" export default class MyInput extends Component { ...
- linux (06) redis安装
redis安装 一.在linux安装redis,通过源码编译安装redis 1.下载源码包 wget http://download.redis.io/releases/redis-4.0.10.ta ...
- 201871010126 王亚涛《面向对象程序设计(Java)》第十二周学习总结
内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...
- 莫烦TensorFlow_09 MNIST例子
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- B1043 输出PATest (20 分)
一.技术总结: 对于哈希字符处理方式,一般是用一个数组存储字符出现的次数,然后再考虑后续. 同时,在输出时,比如这题要输出指定几个字符,我们可以首先统计下这几个字符一共出现的次数sum,然后输出一个就 ...
- Codeforces Round #576 (Div. 1)
Preface 闲来无事打打CF,就近找了场Div1打打 这场感觉偏简单,比赛时艹穿的人都不少,也没有3000+的题 两三个小时就搞完了吧(F用随机水过去了) A. MP3 题意不好理解,没用翻译看了 ...
- [LeetCode] 402. Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- Spring Boot 知识笔记(整合Redis)
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 热情组——项目冲刺 Day4
项目相关 作业相关 具体描述 班级 班级链接 作业要求 链接地址 团队名称 热情组 作业目标 实现软件制作,以及在福大的传播 Github链接 链接地址 SCRUM部分: 成员昵称 昨日目标 开始时间 ...