【发布时间】:2021-03-04 08:32:46
【问题描述】:
我对复杂性有点陌生,并试图确定以下过程的算法复杂性,该过程计算给定字符串是否是另一个给定字符串的旋转(即,如果“bcdea”是“ abcde”)。我很确定这会分解为 O(n),因为函数中的每次检查在最坏的情况下都会分解为 n(我认为!)。听起来对吗?
public class Rotate {
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "bcdea";
System.out.println(isRotation(str1, str2));
}
public static boolean isRotation(String str1, String str2) {
if(str1 == null || str2 == null){
return false;
}
System.out.println(str1 + str2);
if(str1.length() == str2.length() && (str1 + str2).indexOf(str2) > 0){
return true;
}
return false;
}
}
【问题讨论】:
-
算法复杂度不衡量运行时间。
-
呸,抱歉,我指的是算法复杂度,而不是运行时间。
标签: java time-complexity