C# solution: covert to long


  • 0
    B
    public class Solution 
    {
        public int Reverse(int x) 
        {
            // 123 -> 321
            // -123 -> -321
    
            var result = Reverse((long)x);
    
            if (result >= int.MinValue && result <= int.MaxValue)
            {
                return (int)result;
            }
    
            return 0;
        }
    
        private long Reverse(long x)
        {
            long result = 0;
            var cur = x;
            while(cur != 0)
            {
                result = result * 10 + cur % 10;
                cur /= 10;
            }
    
            return result;
        }
    }
    

Log in to reply
 

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