回文判断语法小错误
weyo opened this issue · comments
Yong Wei commented
函数的参数类型是
const char *s
但是在函数内部直接赋值
char *front = s;
会有指针类型不匹配语法错误。当然了,这是算法书,也可以说这些都是伪代码,不要在意这些细节 ╮(╯▽╰)╭
July commented
后来代码改成了
bool IsPalindrome(const char *s, int n)
{
// 非法输入
if (s == NULL || n < 1)
{
return false;
}
int front, back;
// 初始化头指针和尾指针
int front = 0;
back = n - 1;
while (front < back)
{
if (s[front] != s[back])
{
return false;
}
++front;
--back;
}
return true;
}
July commented
晚上又最终修改了下
解法一的代码一如下
bool IsPalindrome(const char *s, int n)
{
// 非法输入
if (s == NULL || n < 1)
{
return false;
}
const char* front,*back;
// 初始化头指针和尾指针
front = s;
back = s+ n - 1;
while (front < back)
{
if (*front != *back)
{
return false;
}
++front;
--back;
}
return true;
}