【LittleDuck_LeetcodeNote】06 - Palindrome Number

更新於 發佈於 閱讀時間約 5 分鐘

【Instagram】 / 【巴哈姆特小屋】 / 【Vocus方格子】 / 【Medium】
喜歡的話就按個愛心或贊助ㄅ。


raw-image

Question and Hints:

Given an integer x, return true if x is a palindrome, and false otherwise.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • 231 <= x <= 231 - 1

Follow up:

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


First Solution:

💡 先將num轉成文字後切割,再用for loop同時從最前面和最後面檢查,
只要有不同的就回傳false,
若for loop順利跑完,表示符合palindrome,return true.
class Solution {
public boolean isPalindrome(int x) {
String[] num = String.valueOf(x).split("");
for(int i = 0; i < num.length; i++){
int end = num.length-1 - i;
if(end - i > 0){
if(!(num[i].equals(num[end])))
return false;
}
}
return true;
}
}

⏰ Runtime: 29 ms (9.89 %)

📦 Memory: 42 MB (55.87 %)



Upgrade Solution:

think of a math problem.

💡 先設定一些一定不符合迴文的條件:
  1. 負數
  2. 大於0且尾數為0
以12321和123321為例,
理論上如果是迴文,從頭到中間值的組成,應該和從尾到中間值的組成一樣,
都是123,1x10x10 + 2x10 + 3,
畫一遍就會很清楚ㄌ,有點像用check從後面往前吃掉一半的感覺。
class Solution {
public boolean isPalindrome(int x) {
if(x<0 || x!=0 && x%10 ==0)
  return false;
int check=0;
while(x>check){
check = check*10 + x%10;
 x/=10;
}
return (x==check || x==check/10);
}
}

⏰ Runtime: 9 ms (99.32 %)

📦 Memory: 42 MB (65.91 %)




留言
avatar-img
留言分享你的想法!
avatar-img
Steven SHIH的沙龍
6會員
36內容數
以英文和日文歌的翻譯為主,並從「歌曲裡的故事」這個角度去翻譯。畢竟自己只有中文算好而已 :D
Steven SHIH的沙龍的其他內容
2023/07/10
Find the Index of the First Occurrence in a String : 找出haystack在字串中第一次出現的index.
Thumbnail
2023/07/10
Find the Index of the First Occurrence in a String : 找出haystack在字串中第一次出現的index.
Thumbnail
2023/07/10
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
2023/07/10
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
2023/07/10
Remove Duplicates from Sorted Array : 將陣列中的重複值移除,並回傳陣列裡的元素總數。
Thumbnail
2023/07/10
Remove Duplicates from Sorted Array : 將陣列中的重複值移除,並回傳陣列裡的元素總數。
Thumbnail
看更多
你可能也想看
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目敘述 Integer to English Words 給定一個整數num 請轉換成對應的的英文數字表達(One, Two, Three, ... 那種數字表達式)
Thumbnail
題目敘述 題目給定我們一顆二元樹的根節點,要求我們計算出從根節點到葉子節點的偽回文路徑路徑有幾條? 偽回文路徑路徑 的定義: 路徑經過重新排列之後,可以形成回文Palindrome,也就是頭尾鏡像對稱。 ​ 例如: 1 -> 3 -> 3 重新排列之後,可以形成 3 -> 1 -> 3
Thumbnail
題目敘述 題目給定我們一顆二元樹的根節點,要求我們計算出從根節點到葉子節點的偽回文路徑路徑有幾條? 偽回文路徑路徑 的定義: 路徑經過重新排列之後,可以形成回文Palindrome,也就是頭尾鏡像對稱。 ​ 例如: 1 -> 3 -> 3 重新排列之後,可以形成 3 -> 1 -> 3
Thumbnail
題目會給定一個陣列,每個數字都恰好出現兩次,只有一個數字是例外。 要求我們找出那個落單也就是例外的數字。
Thumbnail
題目會給定一個陣列,每個數字都恰好出現兩次,只有一個數字是例外。 要求我們找出那個落單也就是例外的數字。
Thumbnail
題目會給定一個字串,問我們裡面最大的回文子字串內容為何? 本題目將用中心展開法來解題
Thumbnail
題目會給定一個字串,問我們裡面最大的回文子字串內容為何? 本題目將用中心展開法來解題
Thumbnail
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
Remove Element : 在陣列中刪除val後,回傳剩餘的元素個數。
Thumbnail
Valid Parentheses : 確認input的括號是否符合成雙成對的規則,符合回傳true,否則回傳false.
Thumbnail
Valid Parentheses : 確認input的括號是否符合成雙成對的規則,符合回傳true,否則回傳false.
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News