Initial commit
This commit is contained in:
46
9/9.cpp
Normal file
46
9/9.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user