bindiego
6/5/2015 - 6:51 AM

Palindrome - 回文

Palindrome - 回文

// 时间复杂度:O(n),空间复杂度:O(1)

/*
 * 同时从字符串头尾开始向中间扫描字串,如果所有字符都一样,那么这个字串就是一个回文。采用这种方法的话,我们只需要维护头部和尾部两个扫描指针即可。
 */

bool IsPalindrome(const char *s, int n)
{
    //非法输入
    if (s == NULL || n < 1) 
        return false;   
    char *front, *back;

    //初始化头指针和尾指针
    front = s; 
    back = s + n - 1; 

    while (front < back) 
    {
        if (*front != *back)
            return false; // 不是回文,立即返回  
        ++front;
        --back;
    }
    return true; // 是回文  
}
// 时间复杂度:O(n),空间复杂度:O(1)
// 先从中间开始、然后向两边扩展查看字符是否相等

bool IsPalindrome2(const char *s, int n)
{
    if (s == NULL || n < 1) 
        return false; // 非法输入  
    char *first, *second;

    int m = ((n >> 1) - 1) >= 0 ? (n >> 1) - 1 : 0; // m is themiddle point of s      
    first = s + m; 
    second = s + n - 1 - m;

    while (first >= s)
    if (s[first--] != s[second++]) 
        return false; // not equal, so it's not apalindrome  
    return true; // check over, it's a palindrome  
}