LeetCode EP.5

9. Palindrome Number

Photo by Annie Spratt on Unsplash

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.

Example 1:

Example 2:

Example 3:

Constraints:

  • -231 <= x <= 231 - 1

Follow up: Could you solve it without converting the integer to a string?

思路

這裡我一開始預想是用前後輪巡的方式,所以直接把 List size/2 所以得到下面 Answer 1 的解法,Else 會這樣寫是因為 Submit 時發現 testcase 裡 0 算是回文的。

Answer 1.

這裡算是第一次解,但後來 Submit 後發現大家 Runtime 怎麼都這麼低,去看了一下別人的寫法,發現有個更優秀的直接使用 reversed 就好了。

Answer 2.

那如果想玩玩看的話也可以參考我的 Github 使用 IntelliJ 就可以 Build 了,裡面也寫了 Test 來驗證。

--

--

陳建維 Ben
工程師求生指南(Sofware Engineer Survival Guide)

喜愛新鮮知識充滿好奇心的Mobile工程師,3C愛好者也是書蟲。連絡信箱:tttw216@gmail.com;目前遷移至我的Blog: https://awilab.com/