Oh, no, I do not find this bug. Using vs2010, that's no problem. Please help me.


  • -2
    I

    public class Solution {
    public int MyAtoi(string str) {
    if (string.IsNullOrEmpty(str))
    {
    return 0;
    }
    int result = 0;
    if (int.TryParse(str, out result))
    {
    return result;
    }
    else
    {
    char[] charArr = str.ToCharArray();
    int factor = 1; int i = 0;

            if (charArr[0] == '+')
            { factor = 1; i = 1; }
            else if (charArr[0] == '-')
            { factor = -1;  i = 1; }
    
            for (; i < charArr.Length; i++)
            {
                int temp = charArr[i] - '0';
                if (temp >=0 && temp <=9)
                {
                    result = result * 10 + temp;
                    continue;
                }
                
                break;
            }
    
            return result * factor;
        }
    }
    

    }


  • 0
    W

    if the string starts with subsequent spaces,your code will not work.


Log in to reply
 

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