C# solution by try{} catch{} to find out whether it overflows


  • 0
    E
    public class Solution
    {
        public int Reverse(int x)
        {
            string temp = x.ToString();
            string str = "";
            if (x >= 0)
            {
                for (int i = temp.Length - 1; i >= 0; i--)
                    str += temp[i].ToString();
                try{ return Convert.ToInt32(str); }
                catch{ return 0; }
            }
            else
            {
                for (int i = temp.Length - 1; i > 0; i--)
                    str += temp[i].ToString();
                try { return 0 - Convert.ToInt32(str); }
                catch { return 0; }
            }
        }
    }

Log in to reply
 

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