Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

列举需要注意的集中情况

1、去空格 trim()

2、首字母为“+”或者“-”,则要记录下来。设一个flag变量,用于存储符号位

3、数字前出现了非数字字符,则返回0

4、在数字中出现了非数字字符,则返回前面的一些数字字符组成的数,后面的直接舍弃

5、存在数字越界的情况,所以要对最后得到的数字进行判断

public static int atoi(String str){
if(str == null || str.length() == 0)
{
return 0;
}
str = str.trim();
int flag = 1;
int start = 0;
long res = 0;
if(str.charAt(0)=='+'){
flag = 1;
start++;
}else if(str.charAt(0)=='-'){
flag = -1;
start++;
}
for(int i = start;i<str.length();i++){
if(Character.isDigit(str.charAt(i))){
return (int)res*flag;
}
res = res*10 + str.charAt(i)-'0';
}
if (flag == 1 && res > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (flag == -1 && (-1) * res < Integer.MIN_VALUE)
return Integer.MIN_VALUE; return (int)(flag * res); }

【LeedCode】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. LeetCode【8】. String to Integer (atoi) --java实现

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

  3. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  4. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. 【Leetcode-easy】String to Integer(atoi)

    题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...

  6. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  7. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

  8. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  9. 【leetcode刷题笔记】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. nopcommerce商城系统--技术与系统需求

    原址:http://www.nopcommerce.com/technologysystemrequirements.aspx 在这里,我们将着眼于nopCommerce的系统要求.为了运行nopCo ...

  2. java文件的I/O

    [原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...

  3. ArcGIS Server中创建的两个账户有什么区别

    新手常常有这样的疑问: 在安装ArcGIS Server的时候创建的账户和在ArcGIS Server Manager上面创建的账户有什么区别? 解答:前者是是为ArcGIS Server创建的操作系 ...

  4. VS 附加进程调试

    1.在IIS 上新建一个项目,制定目录到 项目根目录. 2.制定IIS上指定 主机名称如: vk.com 3. 修改主机HOST 文件:C:\Windows\System32\drivers\etc ...

  5. 处理大并发量订单处理的 KafKa部署总结

    处理大并发量订单处理的 KafKa部署总结 今天要介绍的是消息中间件KafKa,应该说是一个很牛的中间件吧,背靠Apache 与很多有名的中间件搭配起来用效果更好哦 ,为什么不用RabbitMQ,因为 ...

  6. php 计算两个日期的间隔天数

    使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...

  7. HDU2546饭卡---(DP 经典背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory L ...

  8. RPC-Thrift(四)

    Client Thrift客户端有两种:同步客户端和异步客户端. 同步客户端 同步客户端比较简单,以RPC-Thrift(一)中的的例子为基础进行研究源码,先看一下类图. TServiceClient ...

  9. PHP文件操作函数一

    <?php/*Created on 2013-6-26*///判断文件的类型echo filetype("array.php")."<br />&quo ...

  10. 1552: [Cerc2007]robotic sort

    这道题用splay写 先离散化数据保证按题目所述顺序来写 按原序作为键值建树 维护区间最小值去跑 每次将i的位置 和 n的位置x和y找出来后 将x旋转到root y旋转到x的有儿子 这时y的左子树就是 ...