8 - String to Integer (atoi)
JAVA
class Solution { public int myAtoi(String s) { if (s == null) return 0; int n = s.length(); if (n == 0) return 0; int i = 0; while (s.charAt(i) == ' ') { if (++i == n) return 0; } int sign = 1; if (s.charAt(i) == '-') sign = -1; if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i; int res = 0, flag = Integer.MAX_VALUE / 10; for (int j = i; j < n; ++j) { if (s.charAt(j) < '0' || s.charAt(j) > '9') break; if (res > flag || (res == flag && s.charAt(j) > '7')) return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; res = res * 10 + (s.charAt(j) - '0'); } return sign * res; } }
C#
public partial class Solution
{
public int MyAtoi(string str)
{
int i = 0;
long result = 0;
bool minus = false;
while (i < str.Length && char.IsWhiteSpace(str[i]))
{
++i;
}
if (i < str.Length)
{
if (str[i] == '+')
{
++i;
}
else if (str[i] == '-')
{
minus = true;
++i;
}
}
while (i < str.Length && char.IsDigit(str[i]))
{
result = result * 10 + str[i] - '0';
if (result > int.MaxValue)
{
break;
}
++i;
}
if (minus) result = -result;
if (result > int.MaxValue)
{
result = int.MaxValue;
}
if (result < int.MinValue)
{
result = int.MinValue;
}
return (int)result;
}
}
Comments
Post a Comment