Leetcode Reverse Integer problem solution in C# programming

In the Leetcode Reverse Integer problem solution in C# programming Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 – 1], then return 0.

Leetcode Reverse Integer problem solution in C# programming

public class Solution {
    public int Reverse(int x) {
        int iRev=0;
        string strRev = x.ToString();
        strRev = new string (strRev.ToCharArray().Reverse().ToArray());
            
        if(strRev.IndexOf("-") != -1)
        {
            strRev=strRev.Replace("-","");
            strRev="-"+strRev;
        }
        try
        {
            iRev = Int32.Parse(strRev);
        }
        catch
        {
            iRev=0;
        }
        return iRev;
  }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *