From 675f0145ec7fbfa48f6853425d867dd47c05f077 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 22 Jun 2018 11:35:05 +0800 Subject: [PATCH] Create 9.cpp --- LeetCode-CN/9.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 LeetCode-CN/9.cpp diff --git a/LeetCode-CN/9.cpp b/LeetCode-CN/9.cpp new file mode 100644 index 0000000..8a0ebdd --- /dev/null +++ b/LeetCode-CN/9.cpp @@ -0,0 +1,37 @@ +#include +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; + } +};