Initial commit...
This commit is contained in:
parent
f3fc00b319
commit
c238c800b4
|
@ -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
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<startup>
|
||||||
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||||
|
</startup>
|
||||||
|
</configuration>
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{62723402-9D8D-441D-8A23-2766966C4924}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<RootNamespace>P008_String_to_Integer</RootNamespace>
|
||||||
|
<AssemblyName>P008-String-to-Integer</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Solution.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="App.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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")]
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue