Leetcode 7 Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
class Solution {
public int reverse(int x) {
int result = 0;
while (x != 0) {
int mod = x % 10;
x = x / 10;
if (result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10) return 0;
result = result * 10 + mod;
}
return result;
}
}
為了防止溢位,在運算中應該加以判斷進行標記,如果溢位那麼直接返回。
相關文章
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- LeetCode Reverse Integer(007)解法總結LeetCode
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- 「每日一道演算法題」Reverse Integer演算法
- leetcode 344. Reverse StringLeetCode
- Leetcode 151 Reverse Words in a StringLeetCode
- Leetcode 12. Integer to RomanLeetCode
- Leetcode 273 Integer to English WordsLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 25 Reverse Nodes in k-GroupLeetCode
- LeetCode Integer to Roman(012)解法總結LeetCode
- LeetCode Roman to Integer(013)解法總結LeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- 【Leetcode】25.Reverse Nodes in k-GroupLeetCode
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- [Leetcode力扣 25] Reverse Nodes in k-GroupLeetCode力扣
- 【LeetCode】493. Reverse Pairs 翻轉對(Hard)(JAVA)LeetCodeAIJava
- Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)LeetCode字串
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- leetcode-7LeetCode
- Leetcode 504 Base 7LeetCode
- JavaScript reverse()JavaScript
- BUUCTF reverse 3
- 1074 reverse list
- 23.【Reverse】
- Reverse Vowels of a String
- LeetCode 之 JavaScript 解答第150題 —— 逆波蘭表示式求值(Evaluate Reverse Polish Notation)LeetCodeJavaScript
- LeetCode(7)--.反轉整數LeetCode
- Hgame2023 ReverseGAM
- Reverse 攻防世界合集
- Reverse Card (Hard Version)
- hackme 【攻防世界】Reverse
- Integer比較