判断一个整数是否是回文数

判断一个整数是否是回文,并且不能用额外的存储空间。

如22222是回文,1234321也是回文数,即从左往右念和从右往左念是一样的。


int GetNo(int number)
{    
    if ( number < 0 ) return 0;
    if ( number == 0 ) return 1;

    int count = 0;
    while (number != 0) {
        number = number / 10;
        count++;
    }
}

int TenTimes(int times)
{
    int sum = 1;
    if (0 >= times) return 0;

    for (int i = 0; i < times; i++) {
        sum = sum * 10;
    }
}

int GetSpecificBitDigital(int number, int index)
{
    int ret = 0;

    int size = index + 1;
    while (size > 0 ) {
        ret = number % 10;
        number = number / 10;
        size--;
    }

    return ret;

}


bool IsPalindrome(int mynumber) 
{
    if (0 > mynumber) return false;

    int size_no = GetNo(mynumber);
    int start = 0;
    int end = size_no - 1;
    int k = 0;
    for (k = 0; k < size_no / 2; k++) {
        int start_index = start + k;
        int end_index = end - k;
        int start_no = GetSpecificBitDigital(mynumber, start_index);
        int end_no = GetSpecificBitDigital(mynumber, end_index);
        if (start_no != end_no) break;
    }

    return (k == size_no / 2);
}



你可能感兴趣的:(判断一个整数是否是回文数)