class Solution { public String removeDuplicateLetters(String s) { int []tem = new int[26]; for (int i = 0; i < s.length(); i++) { char tems = s.charAt(i); tem[tems - 'a'] ++; } boolean visited[] = new boolean[26]; Stack<Character> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { char get = s.charAt(i); while (!stack.isEmpty() && stack.peek() > get && !visited[get - 'a'] && tem[stack.peek() - 'a'] > 0 ) { visited[stack.peek() - 'a'] = false; stack.pop(); } if(visited[get - 'a']){ tem[get - 'a'] --; continue; } visited[get - 'a'] = true; stack.push(get); tem[get - 'a'] --; } StringBuilder sBuilder = new StringBuilder(); while (!stack.isEmpty()) { sBuilder.append(stack.pop()); } return sBuilder.reverse().toString(); } } 帮我写主函数
时间: 2023-12-14 09:39:04 浏览: 124
好的,以下是一个简单的主函数来测试你的方法:
```
public static void main(String[] args) {
String s = "bcabc";
Solution solution = new Solution();
System.out.println(solution.removeDuplicateLetters(s)); // 输出"abc"
}
```
阅读全文