From 13caa5feeb1dfab303966af3fbf944e76aaee506 Mon Sep 17 00:00:00 2001 From: Darlingnah <463388594@qq.com> Date: Mon, 10 Nov 2025 19:57:30 +0800 Subject: [PATCH 1/2] ylh --- Solution2.java | 12 ++++++------ test.java | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 test.java diff --git a/Solution2.java b/Solution2.java index 3218bd12..7dc29a42 100644 --- a/Solution2.java +++ b/Solution2.java @@ -1,5 +1,5 @@ -/** - * @description: +/* + * /description: * * 给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。 * @@ -21,13 +21,13 @@ public String removeDuplicateLetters(String s) { boolean[] vis = new boolean[25]; int[] num = new int[25]; for (int i = 0; i < s.length(); i++) { - num[s.charAt(i) - ' ']++; + num[s.charAt(i) - 'a']++; } StringBuffer sb = new StringBuffer(); - for (int i = 0; i < s.length()+1; i++) { + for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); - if (!vis[ch - ' ']) { + 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; @@ -39,7 +39,7 @@ public String removeDuplicateLetters(String s) { vis[ch - 'a'] = true; sb.append(ch); } - num[ch - 'a'] += 1; + num[ch - 'a'] -= 1; } return sb.toString(); } diff --git a/test.java b/test.java new file mode 100644 index 00000000..49ec37da --- /dev/null +++ b/test.java @@ -0,0 +1,19 @@ +import java.util.Scanner; +public class test{ + public static void main(String args[]) + { + System.out.println("begin"); + + Scanner scanner = new Scanner(System.in); + + String s = scanner.next(); + + Solution2 solution2 = new Solution2(); + + String remove_s = solution2.removeDuplicateLetters(s); + + System.out.println(remove_s); + + scanner.close(); + } +} \ No newline at end of file From fc36be04f550d16c1fdbaed906558b88bfc7d53a Mon Sep 17 00:00:00 2001 From: Darlingnah <463388594@qq.com> Date: Mon, 10 Nov 2025 20:47:47 +0800 Subject: [PATCH 2/2] ylh02 --- L2023111055_2_Test.java | 39 +++++++++++++++++++++++++++++++++++++++ OSSDP-Lab2.iml | 11 +++++++++++ Solution2.java | 4 +++- test.java | 19 ------------------- 4 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 L2023111055_2_Test.java create mode 100644 OSSDP-Lab2.iml delete mode 100644 test.java diff --git a/L2023111055_2_Test.java b/L2023111055_2_Test.java new file mode 100644 index 00000000..5ffc1869 --- /dev/null +++ b/L2023111055_2_Test.java @@ -0,0 +1,39 @@ +package com.ai; + +import org.junit.Test; + + +import static org.junit.Assert.assertEquals; + +public class L2023111055_2_Test{ + + @Test + public void testSolution2() + { + Solution2 solution = new Solution2(); + + // 测试目的:验证方法能否正确处理无重复字母的字符串 + // 测试用例:输入 "abc",期望输出 "abc" + assertEquals("abc", solution.removeDuplicateLetters("abc")); + + // 测试目的:验证方法能否正确处理有重复字母的字符串 + // 测试用例:输入 "bcabc",期望输出 "abc" + assertEquals("abc", solution.removeDuplicateLetters("bcabc")); + + // 测试目的:验证方法能否正确处理包含多个重复字母的字符串 + // 测试用例:输入 "cbacdcbc",期望输出 "acdb" + assertEquals("acdb", solution.removeDuplicateLetters("cbacdcbc")); + + // 测试目的:验证方法能否正确处理空字符串 + // 测试用例:输入 "",期望输出 "" + assertEquals("", solution.removeDuplicateLetters("")); + + // 测试目的:验证方法能否正确处理单个字符的字符串 + // 测试用例:输入 "a",期望输出 "a" + assertEquals("a", solution.removeDuplicateLetters("a")); + + // 测试目的:验证方法能否正确处理所有字符相同的字符串 + // 测试用例:输入 "aaa",期望输出 "a" + assertEquals("a", solution.removeDuplicateLetters("aaa")); + } +} \ No newline at end of file diff --git a/OSSDP-Lab2.iml b/OSSDP-Lab2.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/OSSDP-Lab2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Solution2.java b/Solution2.java index 7dc29a42..623c4c71 100644 --- a/Solution2.java +++ b/Solution2.java @@ -1,3 +1,5 @@ +package com.ai; + /* * /description: * @@ -16,7 +18,7 @@ * 1 <= s.length <= 104 * s 由小写英文字母组成 */ -class Solution2 { +public class Solution2 { public String removeDuplicateLetters(String s) { boolean[] vis = new boolean[25]; int[] num = new int[25]; diff --git a/test.java b/test.java deleted file mode 100644 index 49ec37da..00000000 --- a/test.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.util.Scanner; -public class test{ - public static void main(String args[]) - { - System.out.println("begin"); - - Scanner scanner = new Scanner(System.in); - - String s = scanner.next(); - - Solution2 solution2 = new Solution2(); - - String remove_s = solution2.removeDuplicateLetters(s); - - System.out.println(remove_s); - - scanner.close(); - } -} \ No newline at end of file