时间复杂度的疑问
查看原帖
时间复杂度的疑问
571265
Infinity_Fantasy楼主2024/5/1 20:53

用 for 循环的复杂度毫无疑问是 O(n)O(n)

for(int i=1,j=n;i<=j;i++,j--){
	if(i==j) cout<<a[i]<<" ";
	else cout<<a[i]<<" "<<a[j]<<" ";
}

可是递归写法

void maxx(int n){
	if(n==1){
		cout<<a[n]<<" ";
		return;
	}
	cout<<a[n]<<" ";
	maxx(n-2);
	cout<<a[n-1]<<" "; 
}

的复杂度我认为也是 O(n)O(n) 为什么二者时间大有不同?

2024/5/1 20:53
加载中...