C# solution for Reverse Integer


  • 0
    S

    public class Solution {
    public int Reverse(int x) {

        int result = 0;
        
        while (x != 0)
        {
            try
            {
                int lastDigit = x % 10;
                result = checked(result * 10 + (lastDigit));
                //Console.WriteLine(checked(result * 10));
                x = x / 10;
            }
            catch(OverflowException)
            {
                result = 0;
                break;
            } 
        }
                                   
        return result;
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.