Solution for C#


  • 0
    E

    ''''
    public class Solution {
    public int MyAtoi(string str) {

            int result = 0;
    
            str = str.Trim();
            char[] chrArr = str.ToCharArray();
            string temp = "";
            bool isNegative = false;
    
            for (int i = 0; i < chrArr.Length; i++)
            {
                if (i == 0 && (chrArr[i] == '-' || chrArr[i] == '+'))
                {
                    if (chrArr[i] == '-')
                    {
                        isNegative = true;
                        temp = "-";
                    }
                    
                    continue;
                }
                else
                {
                    try
                    {
                        result = int.Parse((temp += chrArr[i]));
                    }
                    catch (System.OverflowException) {
    
                         if (isNegative)
                        {
                            result = -2147483648;
                        }
                        else
                        {
                            result = 2147483647;
                        }    
                        
                        break;
                    }
                    catch(Exception)
                    {
                        break;
                    }
                }
            }
    
            return result;
    }
    

    }
    '''


Log in to reply
 

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