mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 25 ms (80.61%), Space: 26.8 MB (43.99%) - LeetHub
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
public class Solution {
|
||||
const int INT_MAX = 2147483647;
|
||||
public int ReverseHelper(int x, int reversed) {
|
||||
if (reversed > (int) (INT_MAX / 10)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (x < 10) {
|
||||
return reversed * 10 + x;
|
||||
}
|
||||
|
||||
return ReverseHelper(x / 10, reversed * 10 + x % 10);
|
||||
}
|
||||
|
||||
public int Reverse(int x) {
|
||||
if (x == - INT_MAX - 1) {
|
||||
return 0;
|
||||
}
|
||||
else if (x < 0) {
|
||||
return -1 * ReverseHelper(Math.Abs(x), 0);
|
||||
}
|
||||
return ReverseHelper(Math.Abs(x), 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user