From c214c8dff4d217025b708c6a21f5db7c9e9e9802 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 2 Nov 2022 22:20:09 +0800 Subject: [PATCH] s0009 --- include/s0009_palindrome_number.hpp | 13 +++++++++++++ src/s0009_palindrome_number.cpp | 8 ++++++++ tests/s0009_palindrome_number.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 include/s0009_palindrome_number.hpp create mode 100644 src/s0009_palindrome_number.cpp create mode 100644 tests/s0009_palindrome_number.cpp diff --git a/include/s0009_palindrome_number.hpp b/include/s0009_palindrome_number.hpp new file mode 100644 index 0000000..4d11182 --- /dev/null +++ b/include/s0009_palindrome_number.hpp @@ -0,0 +1,13 @@ +#ifndef S0009_PALINDROME_NUMBER +#define S0009_PALINDROME_NUMBER + +#include + +using namespace std; + +class Solution { + public: + bool isPalindrome(int x); +}; + +#endif diff --git a/src/s0009_palindrome_number.cpp b/src/s0009_palindrome_number.cpp new file mode 100644 index 0000000..63a3981 --- /dev/null +++ b/src/s0009_palindrome_number.cpp @@ -0,0 +1,8 @@ +#include "s0009_palindrome_number.hpp" + +bool Solution::isPalindrome(int x) { + string s = to_string(x); + string r = s; + reverse(r.begin(), r.end()); + return s == r; +} diff --git a/tests/s0009_palindrome_number.cpp b/tests/s0009_palindrome_number.cpp new file mode 100644 index 0000000..48de79b --- /dev/null +++ b/tests/s0009_palindrome_number.cpp @@ -0,0 +1,24 @@ +#include "s0009_palindrome_number.hpp" + +#include + +TEST(Problem9, Case1) { + int i{121}; + bool o{true}; + Solution solution; + EXPECT_EQ(solution.isPalindrome(i), o); +} + +TEST(Problem9, Case2) { + int i{-121}; + bool o{false}; + Solution solution; + EXPECT_EQ(solution.isPalindrome(i), o); +} + +TEST(Problem9, Case3) { + int i{10}; + bool o{false}; + Solution solution; + EXPECT_EQ(solution.isPalindrome(i), o); +}