C# Solution


  • 0
    K
    public class Solution {
        public int Reverse(int x)
        {
            bool isNegative = x < 0;
            if (isNegative){
                x *= - 1;
            }
            
            int val = 0;
            int prevVal = 0;
            while(x != 0){
                prevVal = val;
                val *= 10;
                val += x % 10;
                if ((val - (x % 10))/10 != prevVal)
                    return 0;
                x /= 10;
            }
            
            return isNegative ? val * -1 : val;
        }
    }

  • 0

    it doesn't work, even if change "int val" to "long val".


Log in to reply
 

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