mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 9.cpp
This commit is contained in:
parent
c8332b9703
commit
675f0145ec
37
LeetCode-CN/9.cpp
Normal file
37
LeetCode-CN/9.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <cstdio>
|
||||
class Solution {
|
||||
public:
|
||||
bool isPalindrome(int x) {
|
||||
/*
|
||||
// Converting to string is too slow!!
|
||||
char buff[64] = { 0 };
|
||||
sprintf(buff, "%d", x);
|
||||
int len = strlen(buff);
|
||||
int L = 0, R = len - 1;
|
||||
while (L < R)
|
||||
{
|
||||
if (buff[L] != buff[R]) return false;
|
||||
++L;
|
||||
--R;
|
||||
}
|
||||
return true;
|
||||
*/
|
||||
if (x < 0) return false;
|
||||
int buff[64] = { 0 };
|
||||
int c = 0;
|
||||
int tx = x;
|
||||
while (tx > 0)
|
||||
{
|
||||
buff[c++] = tx % 10;
|
||||
tx /= 10;
|
||||
}
|
||||
int L = 0, R = c - 1;
|
||||
while (L < R)
|
||||
{
|
||||
if (buff[L] != buff[R]) return false;
|
||||
++L;
|
||||
--R;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user