0

我正在制作一种方法,它将返回一个String[]包含一个字母不同的单词的有效组合。该方法将String包含单词字典的数组作为第一个参数,将另外两个分别包含单词 one 和 two 的字符串作为第二个和第三个参数。

这是我的方法:

public static String[] findCombos(String[] dict, String a, String b){
    char[] wordA = a.toCharArray();
    char[] wordB = b.toCharArray();
    int length = wordA.length;
    List<String> validCombos = new ArrayList<String>();
    Arrays.sort(dict);
    //wordA     
    for(int i = 0; i<length; i++){
        char tmp = wordA[i];
        wordA[i] = 0;
        String tmpWordA = new String(wordA).trim();
        //tmpWordA = tmpWordA + wordA.toString().trim();
        if(Arrays.binarySearch(dict, tmpWordA) >= 0){
            int lengthb = wordB.length;
            String tmpWordB = new String(wordB).trim();
            //tmpWordB = tmpWordB + wordB.toString();
            for(int j = 0; j<lengthb; j++){
                tmpWordB = new StringBuffer(tmpWordB).insert(j ,tmp).toString();
                if(Arrays.binarySearch(dict, tmpWordB) >= 0){
                    validCombos.add(tmpWordA + "\\t" + tmpWordB);//combo found
                }else{
                    wordA[i] = tmp;
                }
            }
        }else{
            wordA[i] = tmp;
        }
    }
    //wordB
    int lengthb = b.length();
    for(int i = 0; i<lengthb; i++){
        char tmp = wordB[i];
        wordB[i] = 0;
        String tmpWordB = new String(wordB).trim();
        //tmpWordB = tmpWordB + wordB.toString().trim();
        if(Arrays.binarySearch(dict, tmpWordB) >= 0){
            int lengtha = a.length();
            String tmpWordA = new String(wordA).trim();
            //tmpWordA = tmpWordA + wordA.toString();
            for(int j = 0; j< lengtha; j++){
                tmpWordA = new StringBuffer(tmpWordA).insert(j, tmp).toString();
                if(Arrays.binarySearch(dict, tmpWordA) >= 0){
                    validCombos.add(tmpWordA + "\\t" + tmpWordB);//combo found
                }else{
                    wordB[i] = tmp;
                }
            }
        }else{
            wordB[i] = tmp;
        }
    }
    String[] res = validCombos.toArray(new String[0]);
    return res;
}

数组已经排序,我确定有问题的元素在数组中,但是搜索不断返回负数并自动分支到else子句。有任何想法吗?这是字典的链接:

字典 - PasteBin

4

1 回答 1

1

您没有删除索引 i 处的字符,而是将索引处的字符替换为i0,该错误假设会破坏您的算法。

使用 StringBuilder 从字符数组中按索引删除字符

String mystring = "inflation != stealing";
char[] my_char_array = mystring.toCharArray();
StringBuilder sb = new StringBuilder();
sb.append(mystring);
sb.deleteCharAt(10);
my_char_array = sb.toString().toCharArray();
System.out.println(my_char_array);             //prints "inflation = stealing"

上面的代码从字符数组中删除了感叹号。

滚动您自己的 java 函数以从字符数组中删除一个字符:

String msg = "johnny can't program, he can only be told what to type";

char[] mychararray = msg.toCharArray();
mychararray = remove_one_character_from_a_character_array_in_java(mychararray, 21);
System.out.println(mychararray);

public char[] remove_one_character_from_a_character_array_in_java(
                           char[] original, 
                           int location_to_remove)
{
    char[] result = new char[original.length-1];
    int last_insert = 0;
    for (int i = 0; i < original.length; i++){
        if (i == location_to_remove)
            i++;

        result[last_insert++] = original[i];
    }
    return result;
}

//The above method prints the message with the index removed.

来源: https ://stackoverflow.com/a/11425139/445131

于 2013-01-08T15:15:57.707 回答