2013-04-30 63 views
2

我尝试了这个问题字谜:查找,并在最后总是元音

使用递归来显示用户输入一个字符串的所有字谜写一个函数,在其所有的元音都位于这样的方式每一个咒语的结尾。 (例如:递归=> Rcrsneuio,cRsnroieu等)优化它。

从这个网站: http://erwnerve.tripod.com/prog/recursion/magic.htm

这是我做了什么:

public static void permute(char[] pre,char[] suff) { 
    if (isEmpty(suff)) { 
     //result is a set of string. toString() method will return String representation of the array. 
     result.add(toString(moveVowelstoEnd(pre))); 
     return; 
    } 
    int sufflen = getLength(suff); //gets the length of the array 
    for(int i =0;i<sufflen;i++) { 
     char[] tempPre = pre.clone(); 
     char[] tempSuf = suff.clone(); 
     int nextindex = getNextIndex(pre); //find the next empty spot in the prefix array 
     tempPre[nextindex] = tempSuf[i]; 
     tempSuf = removeElement(i,tempSuf); //removes the element at i and shifts array to the left 
     permute(tempPre,tempSuf); 
    } 

} 

public static char[] moveVowelstoEnd(char[] input) { 
    int c = 0; 
    for(int i =0;i<input.length;i++) { 
     if(c>=input.length) 
      break; 
     char ch = input[i]; 
     if (vowels.contains(ch+"")) { 
      c++;    
      int j = i; 
      for(;j<input.length-1;j++) 
       input[j] = input[j+1]; 
      input[j]=ch; 
      i--; 
     } 
    } 
    return input; 
} 

最后问题的第一部分是“优化它。我不知道如何优化这个。任何人都可以帮忙吗?

+3

置换辅音,置换元音,附加所得元音设置为辅音组。这将是我想的优化。看起来你在这里可能会这样做? – 2013-04-30 18:56:57

+0

我正在排列所有元音并将元音移动到最后。然后添加到集合中。所以在这里我能看到的是,许多置换是多余的,因为一旦元音移动到最后,它们可能是相同的。 – 2013-04-30 19:06:07

回答

3

集团所有的元音中V

集团所有辅音到瓦特

对于每对字谜的,CONCAT结果

+1

“所有”的元音位于每个谜语的末尾 – 2013-04-30 19:01:40

+0

@ZiyaoWei是的。更新为以这种方式工作。 – ElKamina 2013-04-30 19:06:15

+0

我会试试这种方式。谢谢 – 2013-04-30 19:08:41