diff --git a/P008-String-to-Integer/P008-String-to-Integer.sln b/P008-String-to-Integer/P008-String-to-Integer.sln new file mode 100644 index 0000000..16c3c83 --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26430.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P008-String-to-Integer", "P008-String-to-Integer\P008-String-to-Integer.csproj", "{62723402-9D8D-441D-8A23-2766966C4924}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {62723402-9D8D-441D-8A23-2766966C4924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {62723402-9D8D-441D-8A23-2766966C4924}.Debug|Any CPU.Build.0 = Debug|Any CPU + {62723402-9D8D-441D-8A23-2766966C4924}.Release|Any CPU.ActiveCfg = Release|Any CPU + {62723402-9D8D-441D-8A23-2766966C4924}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/P008-String-to-Integer/P008-String-to-Integer/App.config b/P008-String-to-Integer/P008-String-to-Integer/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/P008-String-to-Integer/P008-String-to-Integer/P008-String-to-Integer.csproj b/P008-String-to-Integer/P008-String-to-Integer/P008-String-to-Integer.csproj new file mode 100644 index 0000000..2666185 --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer/P008-String-to-Integer.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {62723402-9D8D-441D-8A23-2766966C4924} + Exe + P008_String_to_Integer + P008-String-to-Integer + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/P008-String-to-Integer/P008-String-to-Integer/Program.cs b/P008-String-to-Integer/P008-String-to-Integer/Program.cs new file mode 100644 index 0000000..f67861a --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer/Program.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace P008_String_to_Integer +{ + class Program + { + static void Main(string[] args) + { + var solution = new Solution(); + 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.ReadLine(); + } + } +} diff --git a/P008-String-to-Integer/P008-String-to-Integer/Properties/AssemblyInfo.cs b/P008-String-to-Integer/P008-String-to-Integer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0d5c5ab --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("P008-String-to-Integer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Xeon Productions")] +[assembly: AssemblyProduct("P008-String-to-Integer")] +[assembly: AssemblyCopyright("Copyright © Xeon Productions 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("62723402-9d8d-441d-8a23-2766966c4924")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/P008-String-to-Integer/P008-String-to-Integer/Solution.cs b/P008-String-to-Integer/P008-String-to-Integer/Solution.cs new file mode 100644 index 0000000..c83a2ba --- /dev/null +++ b/P008-String-to-Integer/P008-String-to-Integer/Solution.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace P008_String_to_Integer +{ + class Solution + { + /* + * 1047 / 1047 test cases passed. + * Status: Accepted + * Runtime: 205 ms + * Your runtime beats 0.57 % of csharp submissions. :-( + * This is an abomination... + */ + public int MyAtoi(string str) + { + if (string.IsNullOrWhiteSpace(str)) + return 0; + + Match match = Regex.Match(str.Trim(), @"^([\d+-]+)\D?"); + if (!match.Success) + return 0; + string input = match.Groups[0].Value; + int position = 0, output = 0; + bool negative = false; + + if (input[0] == '+') + { + if (input.Length >= 2 && (input[1] < '0' || input[1] > '9')) + return 0; + } + else if (input[0] == '-') + { + if (input.Length >= 2 && (input[1] < '0' || input[1] > '9')) + return 0; + negative = true; + } + else if (!(input[0] >= '0' && input[0] <= '9')) + { + return 0; + } + + for (int i = input.Length - 1; i >= 0; i--) + { + if (input[i] >= '0' && input[i] <= '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; + } + } + else + { + if ((output > 0 || output < 0) && input[i] != '-' && input[i] != '+') + { + position = 0; + output = 0; //reset output?? + } + } + } + + return output; + } + } +} diff --git a/P008-String-to-Integer/README.md b/P008-String-to-Integer/README.md new file mode 100644 index 0000000..c533915 --- /dev/null +++ b/P008-String-to-Integer/README.md @@ -0,0 +1,16 @@ +# String to Integer (atoi) + +## Instructions + +Implement atoi to convert a string to an integer. + +## Requirements + +The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. + +The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. + +If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. + +If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned. +