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(); } } 帮我写int主函数
时间: 2023-12-14 12:39:07 浏览: 148
Java Enum和String及int的相互转化示例
抱歉,我无法为您编写完整的代码。但是,您可以将上述代码复制到您的编辑器中,并在其中添加以下 `main` 函数来测试 `removeDuplicateLetters` 方法:
``` java
public static void main(String[] args) {
String s = "bcabc";
Solution solution = new Solution();
String result = solution.removeDuplicateLetters(s);
System.out.println(result); // Output: "abc"
}
```
请注意,以上代码仅为示例,您可以使用不同的输入字符串来测试该方法。
阅读全文