-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
9.cpp
129 lines (117 loc) · 3 KB
/
9.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
__________________________________________________________________________________________________
16ms
#include <vector>
vector<int> reverse_input;
static const auto speedup = []()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
bool isPalindrome(int x) {
if (x<0) return false;
reverse_input.clear();
while (x) {
reverse_input.push_back(x%10);
x/=10;
}
int back = reverse_input.size()-1;
for (int i=0; i<reverse_input.size()/2; ++i) {
if (back == i) return true;
if (reverse_input[i] != reverse_input[back]) {
return false;
}
--back;
}
return true;
}
};
__________________________________________________________________________________________________
28ms
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
int temp = x;
long int n = 0;
while(x!=0){
n = n*10 + x%10;
x = x/10;
}
if(n==temp) return true;
else return false;
}
};
__________________________________________________________________________________________________
32ms
#include<bits/stdc++.h>
class Solution {
public:
bool isPalindrome(int x) {
unsigned int revNum=0;
int nbr = x;
if (x == 0)
return true;
if (x < 0)
return false;
while (nbr != 0)
{
/*
if (INT_MAX/10 == revNum)
return false;
*/
revNum = revNum*10 + nbr%10;
nbr=nbr/10;
}
if ((revNum == x) || ((revNum/10) == x))
return true;
else
return false;
}
};
__________________________________________________________________________________________________
8000 kb
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPalindrome(int x) {
if(x == 0) return true;
if(x<0 || x%10 == 0) return false;
int sum =0;
while(sum <= x) {
if(sum == x || sum == x/10) return true;
sum *=10;
sum += x%10;
x /= 10;
}
return false;
}
};
__________________________________________________________________________________________________
8012 kb
class Solution {
public:
bool isPalindrome(int x) {
if(x<0|| (x!=0 &&x%10==0)) return false;
int sum=0;
while(x>sum)
{
sum = sum*10+x%10;
x = x/10;
}
return (x==sum)||(x==sum/10);
}
};
static int festio=[](){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(0);
return 0;
}();
__________________________________________________________________________________________________