LeetCode刷題-009回文數
阿新 • • 發佈:2018-05-25
etc spa isp 一個 else false 指正 示例 ret
判斷一個整數是否是回文數。回文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數
示例 1:
輸入: 121
輸出: true
示例 2:
輸入: -121
輸出: false
解釋: 從左向右讀, 為 -121 。 從右向左讀, 為 121- 。因此它不是一個回文數。
示例 3:
輸入: 10
輸出: false
解釋: 從右向左讀, 為 01 。因此它不是一個回文數。
進階:不將整數轉為字符串來解決這個問題
1 class Solution { 2 public: 3 bool isPalindrome(int x) 4 { 5 if(x<0) returnfalse; 6 int x1 = x; 7 int x2 = 0; 8 while(x1) 9 { 10 x2 = x2*10+x1%10; 11 x1 = x1/10; 12 } 13 if(x2==x) return true; 14 else return false; 15 } 16 };
LeetCode刷題-009回文數