我有下面的代码来打印指定字符串的排列。 [为了简化和不丢失专注于什么,我想,让我们假设有没有字符串中的字符复制]算法打印字符串的排列 - 运行时间分析
public static int count = 0;
public static void permute(String soFar, String strLeft) {
if(strLeft.length() == 1) {
soFar += strLeft;
System.out.println(++count + " :: " + soFar);
}
for(int i=0; i<strLeft.length(); i++) {
permute(soFar + strLeft.charAt(i), strLeft.substring(0,i) + strLeft.substring(i+1));
}
}
/**
* @param args
*/
public static void main(String[] args) {
String input = "abcd";
permute("",input);
}
我想弄清楚这个代码的运行时间。
我至今思念链: 想写一个复发,
T(n) = n * T(n-1) + O(n) for n > 1
= 1 for n = 1
有n个!排列,但递归调用的次数大于n !.实际上,对于输入字符串为“abcd”的例子,即4个字符,排列函数的调用次数为65.
任何关于如何查找此代码的运行时间的指针?
什么是更大 - 电话给该方法的数字或数字的,它将其运行时的输出量? – Makoto 2013-03-05 02:15:34
重复/类似的问题? http://stackoverflow.com/questions/11425344/what-is-the-complexity-big-o-of-this-algorithm – apnorton 2013-03-05 02:16:27
@anorton - 是的,这看起来像一个重复。谢谢!我正在通过另一个线程的答案,并将其标记为已关闭。 – 2013-03-05 02:18:01