Solution in C# which auto-hanldes integer overflow


  • 0
    C

    public class Solution {
    public int Reverse(int x) {
    int z= 0;
    try
    {
    z = checked(x);
    String intToStr = x.ToString();
    int len = intToStr.Length - 1;
    int min = 0;
    if(intToStr[0].ToString() == "-")
    {
    min = 1;
    interm = "-";
    }
    String result = "";
    for(int i = len; i >= min; i--)
    result+=intToStr[i].ToString();
    result = interm + result;
    return Int32.Parse(result);
    }
    catch(Exception ex){
    return 0;
    }

        }
    

    }


Log in to reply
 

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