gpt4 book ai didi

java - 计算递归算法的最坏情况运行时间复杂度

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:20:56 26 4
gpt4 key购买 nike

在 clasas 中,我已经开始学习如何计算各种算法的运行时复杂度函数,但我发现这很难。我正在尝试计算下面我的递归算法的最坏情况下的运行时间复杂度。

目前我选择我的基本操作是比较两个字符的索引,它发生在 if 语句中。但是这个 if 语句是嵌套的,我不确定这如何影响递归算法中的 t(n)。

我认为最坏情况下的运行时间复杂度是 t(n) = N(N-1) = N^2 -1 或者只是 O(n)=N^2 是正确的吗? 我认为在最坏的情况下,每个 n 个字符都将在外部 if 语句中进行检查,这意味着将在内部 if 语句中比较 n-1 个字符,因此我得到了这个逻辑。

public class StringShuffleTest {

public static boolean isOrderedShuffle(String a, String b, String c){

//variables for the size of Strings a, b and c.
int n = a.length();
int m = b.length();
int len = c.length();

//if the length of c is not the length of a + b, return false.
if (len != (n + m)){
return false;
}

//if String c contains String b as a substring, then remove String b from c and make m = 0.
//This statement avoids errors when dealing with Strings with very similar characters.
if (c.contains(b)){
c = c.replace(b, "");
m = 0;
}

//if the length of a or b is 0, and c equals a or b, return true, otherwise,
//return false.
if (n == 0 || m == 0){
if (c.equals(a) || c.equals(b)){
return true;
}
else
return false;
}

//if String a has length 1, remove a from String c and make String a empty.
if (n == 1){
c = c.substring(0, c.indexOf(a.charAt(0))) + c.substring(c.indexOf(a.charAt(0)) +1);
a = "";
return isOrderedShuffle(a, b, c);

}

//An ordered shuffle of two given strings, a and b, is a string that can be formed by interspersing
//the characters of a and b in a way that maintains the left-to-right order of the characters from each
//string.

//Recursive algorithm to determine if String c is an ordered shuffle of a and b.
else
if (c.indexOf(a.charAt(0)) >= 0){

int indexOfFirsta = c.indexOf(a.charAt(0));
int indexOfSeconda = c.indexOf(a.charAt(1));

if (indexOfFirsta <= indexOfSeconda){
c = c.substring(0, indexOfFirsta) + c.substring(indexOfFirsta +1);
a = a.substring(1, n);
System.out.println(a);
System.out.println(c);
return isOrderedShuffle(a, b, c);
}

else
if (c.indexOf(b.charAt(0)) >= 0){
int indexOfFirstb = c.indexOf(b.charAt(0));
int indexOfSecondb = c.indexOf(b.charAt(1));

if (indexOfFirstb <= indexOfSecondb){
c = c.substring(0, indexOfFirstb) + c.substring(indexOfFirstb +1);
b = b.substring(1, m);
System.out.println(b);
System.out.println(c);

return isOrderedShuffle(a, b, c);

}
}

}
return false;
}

public static void main(String[] args) {

System.out.println(StringShuffleTest.isOrderedShuffle("abc", "def", "abedcf"));

}

}

最佳答案

如果您将时间复杂度分析分解成多个部分,它会有所帮助。

我们知道,每次调用 isOrderedShuffle 时,至少会删除一个字符。现在让我们假设每次调用 isOrderedShuffle 的复杂度是 C。

T(n) = T(n-1) + C

现在我们需要弄清楚 C 是什么。为此,您需要弄清楚函数中最复杂的操作是什么。这种情况下,我们可以看看String的indexOf函数。当以一个字符作为参数调用 indexOf 时,时间复杂度为 O(n),其中 n 是我们正在搜索的字符串的长度(如果有兴趣,请参阅 this 答案)。在您的算法中,字符串是 c。因此,我们假设长度为 n。 indexOf 被调用的次数是常数。

C = O(n)

因此,

T(n) = T(n-1) + n

我会让你把它变成一个封闭的形式。

关于java - 计算递归算法的最坏情况运行时间复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com