-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_number.cpp
56 lines (52 loc) · 1.16 KB
/
palindrome_number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* =====================================================================================
*
* Filename: palindrome_number.cpp
*
* Description: 9. Palindrome Number.
* https://leetcode.com/problems/palindrome-number/
*
* Version: 1.0
* Created: 01/31/2023 17:21:40
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <array>
#include <cstdio>
#include <string>
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) {
return false;
}
std::array<int, 10> digits;
int j = -1;
int y = x;
while (y > 0) {
digits[++j] = y % 10;
y /= 10;
}
int i = 0;
while (i < j) {
if (digits[i++] != digits[j--]) {
return false;
}
}
return true;
}
};
int main(int argc, char* argv[]) {
int x = 10;
if (argc > 1) {
x = std::stoi(std::string(argv[1]));
}
const bool is_pal = Solution().isPalindrome(x);
printf("%d is palindrome? %s\n", x, (is_pal ? "Yes" : "No"));
return 0;
}