julycoding / The-Art-Of-Programming-By-July-2nd

本项目曾冲到全球第一,干货集锦见本页面最底部,另完整精致的纸质版《编程之法:面试和算法心得》已在京东/当当上销售

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

回文判断语法小错误

weyo opened this issue · comments

函数的参数类型是

const char *s

但是在函数内部直接赋值

char *front = s;

会有指针类型不匹配语法错误。当然了,这是算法书,也可以说这些都是伪代码,不要在意这些细节 ╮(╯▽╰)╭

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;
}
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;
}