C++11新特性std::make_tuple的使用
阿新 • • 發佈:2020-10-06
題目描述:
難度簡單2238收藏分享切換為英文接收動態反饋
給出一個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。
示例 1:
輸入: 123
輸出: 321
示例 2:
輸入: -123
輸出: -321
示例 3:
輸入: 120
輸出: 21
注意:
假設我們的環境只能儲存得下 32 位的有符號整數,則其數值範圍為 [−231, 231 − 1]。請根據這個假設,如果反轉後整數溢位那麼就返回 0。
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/reverse-integer
程式碼:
class Solution { public int reverse(int x) { boolean flag = true; if(x < 0){ flag = false; x = -x; } int res = 0; while(x > 0){ //如果當前的res大於極限值/10,那麼下面乘10肯定爆掉 if (res > Integer.MAX_VALUE / 10) return 0; res = res * 10 + x % 10; x /= 10; } if(flag) return res; else return -res; } } }