Solution in C#


  • 0
    A

    My C# solution

      public int Reverse(int x)
            {
    
                int a = 1;
                int cnt = 0;
                int y = x;
                while (x != 0)
                {
                    x = x / 10;
                    cnt++;
                }
                x = y;
                int ans = 0;
                for (int i = cnt; i > 0; i--)
                {
                    try
                    {
                        checked
                        {
                            y = x % 10;
                            x = x / 10;
                            ans = ans + y * int.Parse((Math.Pow(10, (i - 1))).ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
                return ans;
            }
    

Log in to reply
 

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