实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况!

String to Integer: Case分析

  1. 正常数字

Sample:”123”,”0”,”1” ,"-1"

  1. 普通特殊字符:

Sample: "000","001","-001"

  1. 正负号问题
    1. 含有正负号

Sample: " -123", " +123", "-123", "+123","--123","++123","  -004500"

  1. 空格问题
    1. 含有空格

Sample: " 123","123 123","123 "

  1. 包含特殊符号
    1. 字母,其它特殊符号

Sample: "*123","*abc","~123","123~", "a123","12a3", "12+3","12-3"

  1. 空的字符串

Sample: string.Empty,null,""," "

  1. 边界问题
    1. 正常或者超过Int32最大和最小值,输出最大 或最小Int32

Sample: "-2147483648","2147483647"

Sample: "-214748364800","214748364700"

Snapshot:

Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//http://www.cnblogs.com/binyao/p/5026406.html namespace StringtoInteger
{
class Program
{
static void Main(string[] args)
{
string[] str = { string.Empty,null,""," ",
"","","","","","-1","-001",
"","123 123","123 ",
" -123", " +123",
"-123", "+123","--123","++123"," -004500",
"*123","*abc","~123","123~",
"a123","12a3",
"12+3","12-3",
"-2147483648","",
"-214748364800","" };
StringtoInteger(str);
} public static void StringtoInteger(string[] str)
{
Console.WriteLine("StringtoInteger Result is:");
foreach (string s in str)
{
Console.Write("Test Data:{0}", s);
Console.WriteLine(" Result:{0}", StringtoInteger(s));
}
} public static int StringtoInteger(string str)
{
int sign = ;
int i = ;
int result = ; if (string.IsNullOrEmpty(str))
{
return result;
} while (i < str.Length && ((str[i] >= '' && str[i] <= '') || str[i] == ' ' || str[i] == '-' || str[i] == '+'))
{
if (str[i] == ' ' && (result == && sign == ))
{
i++;
}
else if (str[i] == '+' && (result == && sign == ))
{
sign = ;
i++;
}
else if (str[i] == '-' && (result == && sign == ))
{
sign = -;
i++;
}
else if (str[i] >= '' && str[i] <= '')
{
if (result > (int.MaxValue - (str[i] - '')) / )
{
if (sign == || sign == )
return int.MaxValue;
return int.MinValue;
} result = result * + str[i] - '';
i++;
}
else
{
if (sign == )
return result;
return result * sign;
}
} if (sign == )
return result;
return result * sign;
}
}
}

2-String to Integer (atoi)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  5. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  6. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  10. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. python多线程概念

    转自:http://www.cnblogs.com/fnng/p/3489321.html 在使用多线程之前,我们首页要理解什么是进程和线程. 什么是进程? 计算机程序只不过是磁盘中可执行的,二进制( ...

  2. shell常用调试方法

    检查语法 -n选项只做语法检查,而不执行脚本. sh -n script_name.sh 启动调试 sh -x script_name.s 进入调试模式后,Shell依次执行读入的语句,产生的输出中有 ...

  3. Java从零开始学十九(异常)

    一.什么是异常 从字面上讲,就是不正常的现实就是异常. 程序中的异常也是要在程序运行中才会偶尔发生.如果程序还没有运行,编译就报错,这种不叫异常,这种叫编译错误,通常是语法上的错误 二.java中异常 ...

  4. 从零开始学JavaScript二(基本概念)

    基本概念 一.区分大小写 在ECMAScript中的一切(变量.函数名.操作符)都是区分大小写的. 如变量名test和Test分别表示两个不同的变量, 二.标识符 所谓标识符,就是指变量.函数.属性的 ...

  5. Idea Spring-boot gradle lombok

    1:build.gradle compile("org.projectlombok:lombok:1.16.16") 2:idea安装lombok插件 3:设置 4:重启

  6. redis 安装 命令

    安装: http://redis.io/download 在线操作命令:http://try.redis.io/ 命令查询:https://redis.readthedocs.org/en/lates ...

  7. 在LinuxMint中对firefox进行手动安装flash插件

    /*********************************************************************  * Author  : Samson  * Date   ...

  8. axios 请求参数配置说明

    axios的配置项地址参考: https://www.npmjs.com/package/axios { // `url` is the server URL that will be used fo ...

  9. 微信小程序信息展示

    概述 使用第三方在线API模拟数据,进行微信小程序开发.不会后端开发也可以体验微信小程序. 详细 代码下载:http://www.demodashi.com/demo/10719.html 一.准备工 ...

  10. PHP-手册阅读笔记

    1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...