316:去除重复字母

This commit is contained in:
huangge1199 2021-04-20 15:49:37 +08:00
parent 90bd09b1c1
commit ad099f533f
4 changed files with 111 additions and 61 deletions

View File

@ -0,0 +1,82 @@
//给你一个字符串 s 请你去除字符串中重复的字母使得每个字母只出现一次需保证 返回结果的字典序最小要求不能打乱其他字符的相对位置
//
// 注意该题与 1081 https://leetcode-cn.com/problems/smallest-subsequence-of-distinct
//-characters 相同
//
//
//
// 示例 1
//
//
//输入s = "bcabc"
//输出"abc"
//
//
// 示例 2
//
//
//输入s = "cbacdcbc"
//输出"acdb"
//
//
//
// 提示
//
//
// 1 <= s.length <= 104
// s 由小写英文字母组成
//
// Related Topics 贪心算法 字符串
// 👍 518 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//316:去除重复字母
public class RemoveDuplicateLetters {
public static void main(String[] args) {
//测试代码
Solution solution = new RemoveDuplicateLetters().new Solution();
System.out.println(solution.removeDuplicateLetters("bcabc"));//abc
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String removeDuplicateLetters(String s) {
boolean[] vis = new boolean[26];
int[] num = new int[26];
for (int i = 0; i < s.length(); i++) {
num[s.charAt(i) - 'a']++;
}
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (!vis[ch - 'a']) {
while (!stack.empty() && stack.peek() > ch) {
if (num[stack.peek() - 'a'] > 0) {
vis[stack.peek() - 'a'] = false;
stack.pop();
} else {
break;
}
}
vis[ch - 'a'] = true;
stack.push(ch);
}
num[ch - 'a'] -= 1;
}
StringBuilder sBuilder = new StringBuilder();
while (!stack.empty()) {
sBuilder.insert(0, stack.pop());
}
s = sBuilder.toString();
return s;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>给你一个字符串 <code>s</code> ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 <strong>返回结果的字典序最小</strong>(要求不能打乱其他字符的相对位置)。</p>
<p><strong>注意:</strong>该题与 1081 <a href="https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters">https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters</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 <= 10<sup>4</sup></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>👍 518</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

View File

@ -1,60 +0,0 @@
### 解题思路
执行用时2 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗39 MB, 在所有 Java 提交中击败了9.73%的用户
### 代码
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode res = new ListNode(0);
ListNode helper = res;
int temp = 0;
int up = 0;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + up;
if ( sum > 9) {
temp = sum - 10;
up = 1;
} else {
temp = sum;
up = 0;
}
helper.next = new ListNode(temp);
helper = helper.next;
l1 = l1.next;
l2 = l2.next;
}
ListNode helper2 = l1 == null ? l2 : l1;
while (helper2 != null) {
int sum = helper2.val + up;
if ( sum > 9) {
temp = sum - 10;
up = 1;
} else {
temp = sum;
up = 0;
}
helper.next = new ListNode(temp);
helper = helper.next;
helper2 = helper2.next;
}
if (up == 1) {
helper.next = new ListNode(up);
helper = helper.next;
}
return res.next;
}
}
```