消除字符串中的重复字符

Eliminating repeated characters in a string

你好,我正在尝试让这部分工作,以便当输入是一个字符串,例如 "aaabbbccdddeef" 时,输出是 "abcdef"。我知道那里有解决方案,但令我困扰的是这不起作用,我不明白为什么。如果有人能帮我理解为什么这段代码不起作用,我将不胜感激。

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    char[] store = new char[str.length()]; 
    int count =0;

    for(int i=0; i<str.length();i++) {

        for (int j=0; j<str.length(); j++) {

                if(str.charAt(i)==store[j] ){
                    count+=1;//when character not stored keep count to offset store position
                    break;
                }else {store[i-count] = str.charAt(i); count = 0;}
            }
        }
    System.out.println(str);
    System.out.print(store);

这可以是一个简单的解决方案,也可以是一个非常复杂的解决方案。

我认为我的解决方案非常简单: 制作一个遍历整个字符串长度的 for 循环 然后使用 index of + charAt 来判断一个字符串是否重复超过 1 创建名为 temp 的新字符串。 然后,如果是这样,请删除这些字符中的每一个,只留下一个 然后打印 temp

String.indexOf('a');

另一种方法是附加到不存在的 StringBuilder

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: " );
    String str = input.nextLine();
    StringBuilder store = new StringBuilder ();

    for(int i=0; i<str.length();i++) {
        if (!store.toString().contains(Character.toString(str.charAt(i)))) {
            store.append(str.charAt(i));
        }
    }
    System.out.println(str);
    System.out.print(store);

需要更改第二个 for 循环内的逻辑。您必须在第二个 for 循环中迭代 store 而不是 str。检查我的解决方案:

    Scanner input = new Scanner(System.in);
    System.out.print("please enter the string of characters: ");
    String str = input.nextLine();
    char[] store = new char[str.length()];
    int count = 0;
    boolean charInStore = false;
    for (int i = 0; i < str.length(); i++) {
        charInStore = false;
        for (int j = 0; j < store.length; j++) {
            if (str.charAt(i) == store[j]) {
                charInStore = true;
                break;
            }
        }
        if (!charInStore) {
            store[count] = str.charAt(i);
            count++;
        }
    }
    System.out.println(str);
    System.out.println(new String(store).trim());

您确实需要利用案头检查来更好地了解您的代码在做什么...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
|    0 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    1 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    2 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    3 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    4 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    0 |    5 | aaabbb | [a,  ,  ,  ,  ,  ] |     0 |
|    1 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    2 |    0 | aaabbb | [a,  ,  ,  ,  ,  ] |     2 |
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
|    5 |    0 | aaabbb | [a, b,  , b, b,  ] |     0 |
|    5 |    1 | aaabbb | [a, b,  , b, b,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

核心问题是store[i - count] = str.charAt(i);。在您了解发生了什么之前,它可能并不明显。

让我们仔细看看事情开始出错的地方...

+------+------+--------+--------------------+-------+
|  i   |  j   |  str   |       store        | count |
+------+------+--------+--------------------+-------+
| --- break                                         |
|    3 |    0 | aaabbb | [a, b,  ,  ,  ,  ] |     0 |
|    3 |    1 | aaabbb | [a, b,  ,  ,  ,  ] |     1 |
| --- break                                         |
|    4 |    0 | aaabbb | [a, b,  , b,  ,  ] |     0 |
|    4 |    1 | aaabbb | [a, b,  , b,  ,  ] |     1 |
| --- break                                         |
+------+------+--------+--------------------+-------+

好的,当 i = 4 并且 j0

  • str.charAt(i) = b
  • store[j] = a
  • count = 0

所以,b != a,所以你使用 store[i - count],等同于 store[4 - 0] 并存储 str.charAt(i)(或 b) 那时

事情从此失控。

"basic" 问题是 count 循环之间没有关联。无论如何,我也会质疑是否需要两个循环