Leetcode 9. Palindrome Number

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Palindrome Number

2. Solution

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) {
return false;
}
int m = x;
int y = 0;
while(m) {
y = y * 10 + m % 10;
m /= 10;
}
return x == y;
}
};
  • Version 2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class Solution {
    public:
    bool isPalindrome(int x) {
    if(x < 0 || (x != 0 && x % 10 == 0)) {
    return false;
    }
    int m = x;
    int y = 0;
    while(m) {
    y = y * 10 + m % 10;
    m /= 10;
    }
    return x == y;
    }
    };

Reference

  1. https://leetcode.com/problems/palindrome-number/description/
如果有收获,可以请我喝杯咖啡!