Solution in C#


  • 0
    S

    public class Solution {
    public int Reverse(int x) {
    bool isNegative = false;
    if(x < 0)
    {
    isNegative = true;
    x = -x;
    }
    int reverse = 0;
    int reversedNum = 0;
    while(x!=0)
    {
    int temp = x%10;
    reverse = (reverse * 10) + temp;

                if((reverse - temp)/10 != reversedNum)
                {
                    Console.WriteLine("Overflow");
                    return 0;
                }
            reversedNum = reverse;
            x = x/10;
            
        }
        return (isNegative == true)? -reverse : reverse;
        
    }
    

    }


Log in to reply
 

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