LeetCode——回文数

LeetCode——回文数_第1张图片

暴力解:

 bool isPalindrome(int x) { 
   if(x < 0)
        return false;
    vector vec;
    int  number = x;
    int item = x % 10, rex = 0;
    while(x != 0)
    {
        vec.push_back(item);
        x = x / 10;
        item = x  % 10;
    }
    for (int const &num : vec)
    {
        rex = rex * 10 + num;
    }
    if (rex == number)
        return true;
    else
        return false;
}

仅用一半数字:

bool isPalindrome(int x) {
    int reverseNum = 0;
    if(x < 0 | ((x % 10 == 0) & x != 0))
        return false;
    while(reverseNum < x)
    {
        reverseNum = reverseNum * 10 + (x % 10);
        x /= 10;
    }
        return (reverseNum / 10) == x ||reverseNum  == x;
}

 

你可能感兴趣的:(LeetCode)