Miscellaneous - About strlen () use

I found something a very pit.

Look at the following two codes:

//code1
char s[N];
for(int i=0;i<strlen(s);i++)Do();
//code2
char s[N];
for(int i=0;s[i];i++)Do();

Significance are circulating again strings. but! The former is the complexity of O (N 2 ) , which is O (N) is.

Because the strlen () function time complexity is O (N) of! !

So if you want to use, then we must first put forward to put in front len ​​= strlen (s). Do not like the code as 1! !


 

massacre

Code 1: TLE60 point:

After the change 1984ms-> 85ms

Code 2: 100:

 

Guess you like

Origin www.cnblogs.com/Tieechal/p/11606191.html