Leetcode Exercise 3

Palindrome Number

Wendy Wu
W-Learning Note
2 min readNov 22, 2018

--

Description

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

判斷整數是否為迴文數,如果數字從後面讀過來和原來一樣就表示是迴文數

Example

構思

  1. 先判斷是否小於0,若小於0,直接回傳false
  2. 將數字轉成string
  3. 和該string反轉做比較,並回傳結果

實作程式碼

to_s(base=10):

Returns a string containing the place-value representation of int with radix base(between 2 and 36).

to_s

reverse:

Returns a new string with the characters from str in reverse order.

reverse

結果

beats 100.00% 😀

--

--