给定一个长度为 n 的字符串,请编写一个函数判断该字符串是否回文。如果是回文请返回true,否则返回false。
字符串回文指该字符串正序与其逆序逐字符一致。
数据范围:
要求:空间复杂度
,时间复杂度
"absba"
true
"ranko"
false
"yamatomaya"
false
"a"
true
字符串长度不大于1000000,且仅由小写字母组成
bool judge(char* str ) {
// write code here
for (int i = 0; i < strlen(str) / 2; i++) {
if (str[i] != str[strlen(str) - i - 1]) {
return false;
}
}
return true;
} bool judge(char* str ) {
// write code here
int i = 0, j = strlen(str) - 1;
while (i < j) {
if (str[i] == str[j]) i++, j--;
else return false;
}
return true;
} 为啥上面那个超时?????感觉他的复杂度还低点呢……
bool judge(char* str ) {
// write code here
int left,right;
int i = 0;
int n = strlen(str);
left = 0;
right = n-1;
while(left<right)
{
if(str[left]!=str[right])
{
return false;
}
left++;
right--;
}
return true;
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param str string字符串 待判断的字符串
* @return bool布尔型
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
bool judge(char* str ) {
// write code here
int i,c=0;
int length=strlen(str);
for(i=0;i<length/2;i++){
if(str[i]!=str[length-i-1]){
return false;
}
}
return true;
}