我想请教,如果没有找到一个字符串的一个更有效的方法根据其字母顺序排列上,像下面我的代码。 我用绳子长达16个字符,并且数据量巨大的工作,并运行我的程序需要太多的时间和内存。查找字符串‘置换’基于字母顺序
问题的基本表示
输入:拼音
输出:16752348
所以在单词“字母”字母“A”是字母表中的第一个,它标记索引1,然后是另一“a”的第五位置,将其标记2,然后是“b”在第六位置时,将其标记3等..
在我不使用数字作为索引的代码,而不是的,我使用字符,所以从ASCII值的值65开始。 (因为我使用测试长字符串,但它不会改变主要目的)。所以我的程序的输出将是
输入:拼音
输出:AFGEBCDH
public static String perm(String word){
char[] perm = new char[word.length()];
char[] wordArray = word.toCharArray();
char[] sortedWord = new char[word.length()];
sortedWord = word.toCharArray();
Arrays.sort(sortedWord);
for (int i=0; i<word.length(); i++){
for (int j=0; j<word.length(); j++){
if (sortedWord[i] == wordArray[j]){
perm[j] = (char)(65+i); //from A
wordArray[j] = '.';
j = word.length(); //in case, if the word has more of the tested char, we jump to the end of the cycle
}
}
}
return String.valueOf(perm);
}
public static void main (String [] args){
System.out.println(perm("alphabet"));
}
这是最快的方法,但我也需要为本地语言做。为此,我制作了一个带有相应语言字母表的查找表。 – preem