2-String to Integer (atoi)
实现atoi这个函数, public int atoi(String str),传入字符串str可以返回整数,请仔细考虑一下字符串的各种情况!
String to Integer: Case分析
- 正常数字
Sample:”123”,”0”,”1” ,"-1"
- 普通特殊字符:
Sample: "000","001","-001"
- 正负号问题
- 含有正负号
Sample: " -123", " +123", "-123", "+123","--123","++123"," -004500"
- 空格问题
- 含有空格
Sample: " 123","123 123","123 "
- 包含特殊符号
- 字母,其它特殊符号
Sample: "*123","*abc","~123","123~", "a123","12a3", "12+3","12-3"
- 空的字符串
Sample: string.Empty,null,""," "
- 边界问题
- 正常或者超过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)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- 转: 用 Eclipse 平台进行 C/C++ 开发
http://www.ibm.com/developerworks/cn/linux/opensource/os-ecc/index.html
- C#.NET常见问题(FAQ)-方法参数带ref是什么意思
写两个相同的方法,但是参数一个带ref,一个不带,从测试结果可以发现,a变量在ModifyValueByref之后发生了改变,而ModifyValueByvalue没效果 更多教学视频和资料下 ...
- 如何用代码组织多个Storyboard(故事板)
1. 新建一个Storyboard取名为OtherStoryboard.storyboard 2. 使用下面代码加载 UIStoryboard *newStoryboard = [UIStoryboa ...
- ZH奶酪:Ubuntu14.04 安装Android SDK(SDK tools only)
1.安装JDK(我安装的是Oracle的,而不是openjdk) jdk目录:usr/lib/jvm/java-7-oracle/bin/java 2.下载Android-SDK,在下边的网页选择对应 ...
- UVALive 2949 Elevator Stopping Plan(二分 + 贪心)
ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. ...
- 算法笔记_125:算法集训之编程大题集一(Java)
目录 1 砝码称重 2 公式解析 3 购物券消费方案 4 机器人行走 5 角谷步数 6 矩形区域的交和并 7 矩阵变换加密法 8 控制台表格 9 拉丁方块填数字 10 立方和等式 1 砝码称重 ...
- 笔试题之java基础
Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集合的语法,io 的语法,虚拟机方面的语法,其他.有些题来自网上搜集整理,有些题来自传智 ...
- PHP 表单和用户输入
PHP 的 $_GET 和 $_POST 用于检索表单中的值,比如用户输入. index.php页面 <?php /*时间:2014-09-14 *作者:葛崇 *功能:表单传值小实例 * ...
- tomcat 禁用access.log
修改 server.xml 注释掉,如: <!-- Access log processes all example. Documentation at: /docs/config/valve. ...
- jquery toastmessage (Jquery类似安卓消息提示框)
Do you wanna have some toasts ? jquery-toastmessage-plugin is a JQuery plugin which provides android ...