題目說明:給一個數字並將其反轉兩次後是否等於原本的數字;反轉的定義:1234反轉變成4321,100變成1(將前面的0去除掉)
Case 1:
Input: num = 526
Output: true
Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
Case 2:
Input: num = 1800
Output: false
Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
Case 3:
Input: num = 0
Output: true
Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.
解題的思路:其實從題目對於反轉的定義以及測資就可以發現只要尾數有0的數字,反轉以後其位數(100原本是三位數,反轉過後變成一位樹)一定會跟原本的不一樣,因此反轉第二次時得到的答案也會和原本不一樣,所以這題只是單純要你判斷這個數字除以10的餘數是否為0而已
附上程式碼
Java
class Solution {
public boolean isSameAfterReversals(int num) {
if (num==0) return true;
return num%10!=0;
}
}
Python
class Solution:
def isSameAfterReversals(self, num: int) -> bool:
if num==0:
return True
return not num%10==0