解題心得
1. 負數絕對不可能對稱
2. 不能用string,那就用int array紀錄
3. 比對只需要判斷一半的長度
class Solution { public: bool isPalindrome(int x) { if (x < 0) return false; int arr[10] = { 0 }, count = 0, n = x; while (n > 0) { arr[count] = n % 10; n /= 10; count++; } for (int i = 0; i < count / 2; i++) { if (arr[i] != arr[count - i - 1]) return false; } return true; } };
沒有留言:
張貼留言