比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ && *p2 != ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ && *p2 == ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ ) return 0;
if (*p1 == ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ ) return -1;
if (*p2 == ‘
比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, char * p2) {
while (*p1 != ‘\0’ && *p2 != ‘ \0’ && *p1 == *p2) {
p1++, p2++;
}
if (*p1 == ‘ \0’ && *p2 == ‘ \0’ ) return 0;
if (*p1 == ‘ \0’ ) return -1;
if (*p2 == ‘ \0’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}
’ ) return 1;
return (*p1 - *p2); // it can be negotiated whether the above 3 if’s are
necessary, I don’t like to omit them.
}比较两个字符串,用 O(n)时间和恒量空间。
int strcmp(char * p1, c