下面代码的复杂性是多少
What is the complexity of the code below?
有人能帮我找出以下代码的复杂性吗?
#include<iostream>
#include<string.h>
using namespace std;
//finding the factorial value
int fact(int n)
{
return (n<=1)?1 : n*fact(n-1);
}
int fun(char *str)
{
int rank=1;
int len=strlen(str);
for (int i=0;str[i]!=' ';i++)
{
int count=0;
//finding characters smaller than str[i]
for(int j=i+1;str[j]!=' ';j++)
{
if(str[j]<str[i])
count++;
}
len--;
//finding the rank
rank+=count*fact(len);
}
return rank;
}
int main()
{
char str[]="string";
cout<<endl<<fun(str);
return 0;
}
我认为是O(n^2(。
阶乘函数是O(n(,fun是O(n^2(,所以,实际上它总共是O(n^2(。
相关文章:
- 复制列表初始化的隐式转换的等级是多少
- while循环中while循环的时间复杂度是多少
- 具有未知值时的时间复杂性
- 如何检查一个c++字符串中有多少相同的字符/数字
- C++有多少类型的循环
- 关联容器的下界复杂性:成员函数与非成员函数
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 使用堆查找第K个最大元素的时间复杂性
- 在条件变量中触发错误信号的频率是多少
- 函数的时间复杂度是多少?
- 这个递归函数有什么作用?运行时的复杂性是多少?
- 从 long 转换C++位集构造函数的复杂性是多少?
- 解决这个问题的时间复杂性是多少
- 映射向量的时间复杂性是多少
- 此代码的运行时复杂性是多少
- map/set :: 插入是否提供了正确的迭代器提示,它的复杂性是多少?
- 在排序的std::list中搜索的复杂性是多少
- 对向量调用.end()的复杂性是多少
- 范围变化时的复杂性是多少?
- 下面代码的复杂性是多少