Created
October 11, 2020 12:22
-
-
Save u8989332/0a769c5c11b406a93bded3cb51222d8e to your computer and use it in GitHub Desktop.
LeetCode - 7 Reverse Integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace LeetCode7 | |
{ | |
public class Solution { | |
public int Reverse(int x) { | |
long ans = 0; | |
while(x != 0){ | |
ans = ans * 10 + (x % 10); | |
x /= 10; | |
} | |
return Math.Abs(ans) > Int32.MaxValue ? 0 : (int)ans; | |
} | |
} | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Solution sol = new Solution(); | |
Console.WriteLine(sol.Reverse(123)); | |
Console.WriteLine(sol.Reverse(-123)); | |
Console.WriteLine(sol.Reverse(1234567899)); | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment