1081:不同字符的最小子序列
This commit is contained in:
parent
2db7cd2312
commit
2efb14912d
@ -0,0 +1,101 @@
|
||||
//返回 s 字典序最小的子序列,该子序列包含 s 的所有不同字符,且只包含一次。
|
||||
//
|
||||
// 注意:该题与 316 https://leetcode.com/problems/remove-duplicate-letters/ 相同
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:s = "bcabc"
|
||||
//输出:"abc"
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:s = "cbacdcbc"
|
||||
//输出:"acdb"
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 1000
|
||||
// s 由小写英文字母组成
|
||||
//
|
||||
// Related Topics 栈 贪心算法 字符串
|
||||
// 👍 95 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//1081:不同字符的最小子序列
|
||||
public class SmallestSubsequenceOfDistinctCharacters {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SmallestSubsequenceOfDistinctCharacters().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String smallestSubsequence(String s) {
|
||||
boolean[] isHave = new boolean[26];
|
||||
int[] count = new int[26];
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
count[s.charAt(i) - 'a']++;
|
||||
}
|
||||
Stack<Character> stack = new Stack<>();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
if (!isHave[ch - 'a']) {
|
||||
while (!stack.isEmpty() && stack.peek() > ch) {
|
||||
if (count[stack.peek() - 'a'] <= 0) {
|
||||
break;
|
||||
}
|
||||
isHave[stack.peek() - 'a'] = false;
|
||||
stack.pop();
|
||||
}
|
||||
isHave[ch - 'a'] = true;
|
||||
stack.push(ch);
|
||||
}
|
||||
count[ch - 'a']--;
|
||||
}
|
||||
StringBuilder str = new StringBuilder();
|
||||
while (!stack.isEmpty()) {
|
||||
str.insert(0, stack.pop());
|
||||
}
|
||||
return str.toString();
|
||||
//官网
|
||||
// boolean[] vis = new boolean[26];
|
||||
// int[] num = new int[26];
|
||||
// for (int i = 0; i < s.length(); i++) {
|
||||
// num[s.charAt(i) - 'a']++;
|
||||
// }
|
||||
//
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// for (int i = 0; i < s.length(); i++) {
|
||||
// char ch = s.charAt(i);
|
||||
// if (!vis[ch - 'a']) {
|
||||
// while (sb.length() > 0 && sb.charAt(sb.length() - 1) > ch) {
|
||||
// if (num[sb.charAt(sb.length() - 1) - 'a'] > 0) {
|
||||
// vis[sb.charAt(sb.length() - 1) - 'a'] = false;
|
||||
// sb.deleteCharAt(sb.length() - 1);
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// vis[ch - 'a'] = true;
|
||||
// sb.append(ch);
|
||||
// }
|
||||
// num[ch - 'a']--;
|
||||
// }
|
||||
// return sb.toString();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<p>返回 <code>s</code> 字典序最小的子序列,该子序列包含 <code>s</code> 的所有不同字符,且只包含一次。</p>
|
||||
|
||||
<p><strong>注意:</strong>该题与 316 <a href="https://leetcode.com/problems/remove-duplicate-letters/">https://leetcode.com/problems/remove-duplicate-letters/</a> 相同</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong><code>s = "bcabc"</code>
|
||||
<strong>输出<code>:</code></strong><code>"abc"</code>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong><code>s = "cbacdcbc"</code>
|
||||
<strong>输出:</strong><code>"acdb"</code></pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>贪心算法</li><li>字符串</li></div></div>\n<div><li>👍 95</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user