Improved the abomination...

This commit is contained in:
Brandon Scott 2017-07-02 20:29:07 -05:00
parent c238c800b4
commit 89be652107
2 changed files with 24 additions and 47 deletions

View File

@ -15,6 +15,7 @@ namespace P008_String_to_Integer
Console.WriteLine(solution.MyAtoi("-1024")); Console.WriteLine(solution.MyAtoi("-1024"));
Console.WriteLine(solution.MyAtoi(uint.MaxValue.ToString())); Console.WriteLine(solution.MyAtoi(uint.MaxValue.ToString()));
Console.WriteLine(solution.MyAtoi("-" + uint.MaxValue.ToString())); Console.WriteLine(solution.MyAtoi("-" + uint.MaxValue.ToString()));
Console.WriteLine(solution.MyAtoi("+1"));
Console.ReadLine(); Console.ReadLine();
} }
} }

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace P008_String_to_Integer namespace P008_String_to_Integer
@ -12,69 +11,46 @@ namespace P008_String_to_Integer
/* /*
* 1047 / 1047 test cases passed. * 1047 / 1047 test cases passed.
* Status: Accepted * Status: Accepted
* Runtime: 205 ms * Runtime: 166 ms
* Your runtime beats 0.57 % of csharp submissions. :-(
* This is an abomination...
*/ */
public int MyAtoi(string str) public int MyAtoi(string str)
{ {
if (string.IsNullOrWhiteSpace(str)) string input = str.Trim();
if (input.Length == 0)
return 0; return 0;
Match match = Regex.Match(str.Trim(), @"^([\d+-]+)\D?"); int sign = 1;
if (!match.Success) int output = 0;
return 0; int start = 0;
string input = match.Groups[0].Value;
int position = 0, output = 0;
bool negative = false;
if (input[0] == '+') // sanity checks
if (input[0] == '+' || input[0] == '-')
{ {
if (input.Length >= 2 && (input[1] < '0' || input[1] > '9')) if (input.Length < 2 || (input[1] < '0' || input[1] > '9'))
return 0; return 0;
}
else if (input[0] == '-') sign = input[0] == '-' ? -1 : 1;
{ start = 1;
if (input.Length >= 2 && (input[1] < '0' || input[1] > '9'))
return 0;
negative = true;
} }
else if (!(input[0] >= '0' && input[0] <= '9')) else if (!(input[0] >= '0' && input[0] <= '9'))
{ {
return 0; return 0;
} }
for (int i = input.Length - 1; i >= 0; i--) for(int i = start; i < input.Length; i++)
{ {
if (input[i] >= '0' && input[i] <= '9') int digit = input[i] - '0';
{ if (digit < 0 || digit > 9)
try
{
checked
{
if (!negative)
output += (int)Math.Pow(10, position++) * (input[i] - '0');
else
output -= (int)Math.Pow(10, position++) * (input[i] - '0');
}
}
catch (OverflowException)
{
output = (negative ? int.MinValue : int.MaxValue);
break; break;
}
} if (int.MaxValue / 10 < output || int.MaxValue / 10 == output && int.MaxValue % 10 < digit)
else return sign == 1 ? int.MaxValue : int.MinValue;
{
if ((output > 0 || output < 0) && input[i] != '-' && input[i] != '+') output = 10 * output + digit;
{
position = 0;
output = 0; //reset output??
}
}
} }
return output; return output * sign;
} }
} }
} }