- 316. 去除重复字母
- 321. 拼接最大数
Leetcode
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
q, vis, count = [], set(), Counter(s)
for ch in s:
if ch not in vis:
while q and q[-1] > ch and count[q[-1]]:
# q.pop() # 前一个不唯一,并且比较大,删除。
vis.remove(q.pop())
q.append(ch)
vis.add(ch)
count[ch] -= 1
return ''.join(q)
class Solution {
public String removeDuplicateLetters(String s) {
boolean[] vis = new boolean[26];
int[] num = new int[26];
char[] arr = s.toCharArray();
for (char ch : arr){num[ch - 'a']++;}
StringBuilder sb = new StringBuilder();
for (char ch : arr){
if (!vis[ch - 'a']){
int i = sb.length() - 1;
while (i > -1 && sb.charAt(i) > ch && num[sb.charAt(i) - 'a'] > 0){
vis[sb.charAt(i) - 'a'] = false;
sb.deleteCharAt(i);
i--;
}
vis[ch - 'a'] = true;
sb.append(ch);
}
num[ch - 'a'] -= 1;
}
return sb.toString();
}
}
321. 拼接最大数
Leetcode
class Solution:
def maxNumber(self, nums1, nums2, k):
def pick(nums, k):
q, drop = [], len(nums) - k
for num in nums:
while drop > 0 and q and q[-1] < num:
q.pop()
drop -= 1
q.append(num)
return q[:k]
def merge(A, B):
ans = []
while A or B:
bigger = A if A > B else B
ans.append(bigger[0])
bigger.pop(0)
return ans
return max(merge(pick(nums1, i), pick(nums2, k - i)) for i in range(k+1) if i <= len(nums1) and k-i <= len(nums2))



