1 void reverse( string &s ){ 2 int n = s.size(); 3 for(int i = 0 ; i < n/2 ; i++ )//这个循环走了n/2次,所以o(n) 4 swap( s[i] , s[n-1-i] ); 5 }
1 int count = 1; 2 while (count < n){//cout*2^x=n,x=logn,所以o(logn) 3 count = count * 2; 4 }
1 int i, j; 2 for(i = 1; i < n; i++)//n^3 3 for(j = 1; j < n; j++) 4 for(j = 1; j < n; j++){ 5 ...... 6 }
1 int i, j; 2 for(i = 0; i < n; i++){//n^2 3 for(j = i; j < n; j++){ 4 ..... 5 } 6 }
1 for(int i=2;i*i<=n;i++){//sqrt(n) 2 .... 3 }
0 条评论