46 lines
723 B
C++
46 lines
723 B
C++
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
class Solution {
|
|
public:
|
|
bool isPalindrome(int x) {
|
|
if (x < 0)
|
|
return false;
|
|
|
|
std::vector<int> v;
|
|
v.reserve(32);
|
|
|
|
while (x > 0) {
|
|
int z = x % 10;
|
|
x = x / 10;
|
|
v.emplace_back(z);
|
|
}
|
|
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
|
if (v[i] != v[v.size() - 1 - i])
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
};
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
Solution s;
|
|
|
|
TEST(P9, 1) {
|
|
EXPECT_EQ(s.isPalindrome(121), true);
|
|
}
|
|
|
|
TEST(P9, 2) {
|
|
EXPECT_EQ(s.isPalindrome(-121), false);
|
|
}
|
|
|
|
TEST(P9, 3) {
|
|
EXPECT_EQ(s.isPalindrome(10), false);
|
|
} |