Solution by natesanj in C#


  • 0
    N

    public class Solution {
    public int Reverse(int x) {
    var isNegative = false;
    if(x<0)
    {
    isNegative=true;
    x= x * -1;
    }
    Int32 reverse=0;
    while(x>0)
    {
    try
    {
    checked
    {
    int remainder = x % 10;
    reverse = (reverse * 10) + remainder;
    x = x / 10;
    //Console.WriteLine("{0},{1}, {2}",remainder, reverse, x);
    }
    }
    catch(Exception ex)
    {
    return reverse = 0;
    }
    }

        if(isNegative)
            reverse = reverse * -1;
        
        if(reverse>=Int32.MaxValue || reverse<=Int32.MinValue)
        {
            //Console.WriteLine("true {0}", reverse);
            return 0;
        }
        //Console.WriteLine("false {0},{1},{2}", reverse, Int32.MinValue, Int32.MaxValue);
        return reverse;
    }
    

    }


Log in to reply
 

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