【问题标题】:Remove Duplicate Chars without using an extra array删除重复字符而不使用额外的数组
【发布时间】:2017-12-06 06:27:32
【问题描述】:

我目前正在完成《Cracking the Coding Interview》,我正在寻找一些关于我可以做些什么来纠正这个算法的建议。它似乎正在处理一些测试用例,但它不适用于测试用例 ['a','a','a','b','b','b] 作为输入。任何想法我做错了什么?谢谢!

预期结果 = ['a','b']
实际结果 = ['a']

/**
 * Removes duplicate chars
 * 
 * @param str
 */
public static void removeDuplicates(char[] str) {

    if (str.length < 2) {
        return;
    }

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

        for (int j = 0; j < str.length; j++) {
            if ((str[i] == str[j]) && (i != j)) {
                str[j] = 0;
            }
        }
    }
}

【问题讨论】:

  • 如果您不明白为什么它不起作用,请逐行调试代码。这是一项必不可少的技能。此外,您还没有显示结果应该是什么,也没有显示您实际得到的错误结果。
  • 不知道预期的结果应该是什么,我们怎么知道哪里出了问题?用\0 替换重复的字符似乎不太可能是正确的,但是.....
  • 输入['a','a','a','b','b','b'],您的方法将数组更新为['a','\0','\0','b','\0','\0']。这就是它应该变成的样子吗?如果是,那么您的代码有效。如果不是,那应该是什么?
  • 现在该问题显示了预期和实际输出,您的问题一团糟。标题说“不使用额外数组”,但 Java 数组是固定大小的,因此如果不创建新的较短数组,结果不能是 ['a','b']['a']。请澄清一下,当你这样做时,也许这也会让你自己更清楚。
  • @Andreas 是的 - 应该是这样。出于某种原因,Eclipse 只显示“a”......为了澄清使用额外的数组,我的意思是声明像 StringBuilder 对象之类的东西,然后将非重复项转储到其中。结果,这将“删除重复项”,然后我们可以返回一个“删除”重复项的字符串。但是,由于 char[] 是可变的,我可以删除数组中的项目,而无需创建另一个数组/数据结构。

标签: java arrays algorithm char duplicates


【解决方案1】:

尝试这样做:

public static void removeDuplicates(char[] str) {

    if (str.length < 2) {
        return;
    }

    for (int i = 0; i < str.length; i++) {
        for (int j = 0; j < str.length; j++) {
            System.out.println(i + "-" + j + " = " + str[j]); //added this line
            if ((str[i] == str[j]) && (i != j)) {
                str[j] = 0;
            }
        }
    }
}

我为什么要给你看这个?这将向您展示删除过程的样子,并帮助您更好地理解问题。它实际上工作正常。

我不知道你是怎么得到结果的,因为没有 print 语句,并且没有return 语句。但是我确实找到了一种方法来做到这一点,而无需使用另一个 char 数组(或任何数组)。它只是重构str。看看吧:

public static void main(String[] args) {
    char[] chr = {'a','a','b','c','b','a','b','c'};
    System.out.println(removeDuplicates(chr));
}

public static char[] removeDuplicates(char[] str) {
    if (str.length < 2) {
        return null;
    }
    for (int i = 0; i < str.length; i++) {
        for (int j = 0; j < str.length; j++) {
            if ((str[i] == str[j]) && (i != j)) {
                str[j] = 0;
            }
            if (i == (str.length-1)) {
                str[i] = str[j];
            }
        }
    }
    return str;
}

这个例子给出了输出:

abc

【讨论】:

  • 感谢您的帮助!有趣的是,我复制并粘贴了您提供的示例,我的输出只是“a”。我的 IDE 肯定在说谎。
  • @noobatrilla 太棒了!很高兴我能帮上忙!
【解决方案2】:

考虑下一个(伪)代码:

if (str.length < 2) {
      return;
 }

good = 1; //current number of unique items  

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

    success = 1;
    //scan only unique items
    for (int j = 0; j < good; j++) {
        if ((str[i] == str[j]) {
            success = 0;
            break;  
        }
    }

   //new unique - copy at the final place 
   if (success)  {
      s[good] = str[i];
      good++;   
   }
}
if (good<length)
  str[good] = 0;   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 2011-09-26
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 2014-06-22
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多