标签:rem bsp sig public int leetcode turn break ++
public int MyAtoi(string str) { if (string.IsNullOrEmpty(str)) { return 0; } int sign = 1; int i = 0; for (; i < str.Length; ++i) { if (str[i] != ‘ ‘) { break; } } if (str[i] == ‘+‘) { sign = 1; i++; } else if (str[i] == ‘-‘) { sign = -1; i++; } int digit = 0; for (; i < str.Length; ++i) { if (str[i] < ‘0‘ || str[i] > ‘9‘) { break; } // overflow if (digit * sign > Int32.MaxValue / 10 || (digit * sign == Int32.MaxValue / 10 && (str[i] - ‘0‘) > Int32.MaxValue % 10)) { return Int32.MaxValue; } if (digit * sign < Int32.MinValue / 10 || (digit * sign == Int32.MinValue / 10 && (str[i] - ‘0‘) * sign < Int32.MinValue % 10)) { return Int32.MinValue; } digit = digit * 10 + (str[i] - ‘0‘); } return digit * sign; }
标签:rem bsp sig public int leetcode turn break ++
原文地址:http://www.cnblogs.com/pengdotnet/p/String-to-Integer.html