No.008 String to Integer (atoi)
8. String to Integer (atoi)
- Total Accepted: 112863
- Total Submissions: 825433
- Difficulty: Easy
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.
思路:
本题的难度倒不大,主要是考察的细心程度和对和各种可能的情况是否考虑清楚。题主也是通过三四次提交失败之后才accepted。
主要考虑的情况有以下几种:
- 空串或str == null
- 字符串开头有连续的空格
- 非空格的第一个字符为“+”或“-”或0或其他情况
- 字符串长度可能非常长,甚至转换过来之后超过long类型的范围
- 当遇到非正常字符时,输出之前的结果,eg:-123a245,输出结果为-123
- 对于输出结果超出int类型范围的输出边界值,也就是说输出结果大于int类型最大值,则输出Integer.MAX_VALUE,当输出结果小于int类型最小值时,输出Integer.MIN_VALUE
暂时只考虑了这么多种吧,代码如下:
public int myAtoi(String str) {
//对null和空串情况进行判断
if(str == null || str.length() == 0){
return 0 ;
}
//截除字符串的前面的空格
char [] arr = str.trim().toCharArray() ;
int temp ; //乘子,表示正负
int index ; //表示最高位开始的index
//判断非空有效位的首位的情况
if((arr[0] == '-') && (arr.length > 1)){
temp = -1 ;
index = 1 ;
}else if((arr[0] <= '9') && (arr[0] >= '0')){
temp = 1 ;
index = 0 ;
}else if((arr[0] == '+') && (arr.length > 1)){
temp = 1 ;
index = 1 ;
}else{
return 0 ;
}
long res = 0 ;
for(int i = index ; i < arr.length ; i++){
/*判断每一位是否是数字,不是则直接截断已经处理的结果
* 考虑到int类型的最长有效位数是10位,加上符号位11位,这里再附加一位保险位,
* 超过12位的肯定超出了有效范围我们不继续处理后面的,直接截断*/
if((arr[i] <= '9') && (arr[i] >= '0' && i < 13)){
res = res*10 + temp*(arr[i]-'0') ;
}else{
break ;
}
}
//判断输出的结果是否超出int类型的范围
if(res > Integer.MAX_VALUE ){
return Integer.MAX_VALUE ;
}else if(res < Integer.MIN_VALUE){
return Integer.MIN_VALUE ;
}else{
return (int)res ;
}
}
No.008 String to Integer (atoi)的更多相关文章
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [Leetcode]008.String to Integer (atoi)
public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...
- 008 String to Integer (atoi) 字符串转换为整数
详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 ...
随机推荐
- 【转】 SQL 2005 try catch
1 TRY…CATCH 1.1 用法 TRY…CATCH的语法如下: BEGIN TRY -- TRY 模块 -- 业务处理 END TRY BEGIN CATCH -- CATC ...
- 【转】C#中判断扫描枪输入与键盘输入
提出问题:在收货系统中,常常要用到扫描枪扫描条码输入到TextBox,当条码无法扫描时,需要手工输入.如果是扫描枪输入时,我们将自动去判读条码,而手工输入时,最终需要加按回车键确认后判读条码.这时候我 ...
- (VS) TFS lost mapping suddenly.
家里的网络不是很稳定.今天突然发现 TFS 上所有的 mapping都突然没有了. 尝试去remapping,在Source Control Explorer 中右击源文件,然后选择 Advanced ...
- onclick事件对动态参数类型为字符串的处理
onclick="solveRow("'+row.isbesolve+'")"
- CentOs of Tomcat commands
1.启动tomcat 进入tomcat目录的bin目录下 执行 [root@iZ253lxv4i0Z bin]# ./startup.sh Using CATALINA_BASE: /usr/loca ...
- spark1.4加载mysql数据 创建Dataframe及join操作连接方法问题
首先我们使用新的API方法连接mysql加载数据 创建DF import org.apache.spark.sql.DataFrame import org.apache.spark.{SparkCo ...
- IREP_SOA Integration SOAP概述(概念)
20150827 Created By BaoXinjian
- Windows服务程序和安装程序制作
转:http://www.cr173.com/html/15350_1.html 本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建 ...
- 通讯簿(apple)
ylbtech-dbs:ylbtech-cnblogs(博客园)-2,Admin(用户后台) DatabaseName:Contacts/通讯簿(iOS) 1.A,数据库关系图(Database Di ...
- 值不能为 null 或为空。参数名: linkText
“/”应用程序中的服务器错误. 值不能为 null 或为空.参数名: linkText 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的 ...