Leetcode9_ back to the number

Hahahahahahaha, I’m so happy, today’s code time and memory consumption are half of the official code haha

(Because the official code is written in C#, I use C++, manual dog head)

Title

To determine whether an integer is a palindrome. The palindrome number refers to the same integer in both positive order (from left to right) and reverse order (from right to left).

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false< br>Explanation: Reading from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reading from right to left, it is 01. Therefore it is not a palindrome.

Code

share picture

 1 class Solution {

2 public:
3 bool isPalindrome(int x) {
4 int a = 0;
5 if (x == 0)
6 return true;
7 else if (x <0 || x% 10 == 0)
8 return false;
9 while(x > a){
10 a = a * 10 + x% < span style="color: #800080;">10;
11 x/=10;
12 }
13 return x == a || x = = a / 10;
14 }
15 };

View Code < /div>

share picture

 1 class Solution {

2 public:
3 bool isPalindrome(int x) {
4 int a = 0;
5 if (x == 0)
6 return true;
7 else if (x <0 || x% 10 == 0)
8 return false;
9 while(x > a){
10 a = a * 10 + x% < span style="color: #800080;">10;
11 x/=10;
12 }
13 return x == a || x = = a / 10;
14 }
15 };

View Code< /p>

 1 class Solution {

2 public:
3 bool isPalindrome(int x) {
4 int a = 0;
5 if (x == 0)
6 return true;
7 else if (x <0 || x% 10 == 0)
8 return false;
9 while(x > a){
10 a = a * 10 + x% < span style="color: #800080;">10;
11 x/=10;
12 }
13 return x == a || x = = a / 10;
14 }
15 };

Leave a Comment

Your email address will not be published.