实现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. 解决Windows7 Embedded连接手机问题

    故障现象:正确安装厂商自带的驱动后,插入安卓或iPhone手机,提示找到新硬件,却无法成功安装驱动.在此可以肯定的是:手机驱动无问题,手机.连接线也无问题.看来问题又落到“Embedded”上了! 仔 ...

  2. Hadoop,HBase集群环境搭建的问题集锦(二)

    10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...

  3. 算法笔记_171:历届试题 小朋友排队(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 n 个小朋友站成一排.现在要把他们按身高从低到高的顺序排列,但是每次只能交换位置相邻的两个小朋友. 每个小朋友都有一个不高兴的程度.开始的 ...

  4. Showing a tooltip

    We can provide a balloon help for any of our widgets. #!/usr/bin/python # -*- coding: utf-8 -*- &quo ...

  5. Hibernate日期映射类型

    映 射 类 型 Java类型 标准SQL类型 描    述 date java.util.Date或者java.sql.Date DATE 代表日期,形式为: YYYY-MM-DD time java ...

  6. Fail Fast and Fail Safe Iterators in Java

    https://www.geeksforgeeks.org/fail-fast-fail-safe-iterators-java/ Fail Fast and Fail Safe Iterators ...

  7. struts2接收参数的几种形式

    1.使用属性 HTML: <form action="login" method="post" name="form1"> 用户 ...

  8. python之函数用法getattr()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法getattr() #getattr() #说明: ''' getattr(...) ...

  9. Javascript模块化编程:require.js的用法

    摘自:http://blog.jobbole.com/30046/ 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库 ...

  10. putty简单使用

    一.Putty简介 Putty是一款轻便的远程登录工具,用它可以非常方便的登录到Linux服务器上进行各种操作(命令行方式).Putty完全免费,而且无需安装(双击即可运行),支持多种连接类型(Tel ...