LeetCode--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 ;
}
}
LeetCode--No.008 String to Integer (atoi)的更多相关文章
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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)
public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- 获取当前页面url
function getCurrUrl() { var url = ""; if (parent !== window) { try { url = window.top.loca ...
- nginx_server_location对客户资源的辨别规则
语法:location [ = | ~ | ~* | ^~ ] uri { …一组命令… } http://nginx.org/en/docs/http/ngx_http_core_module.ht ...
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015)
题目链接 : http://codeforces.com/gym/100781/attachments A-Adjoin the Network 题意大概是有多棵树(也可能是一颗),现在要把它们合并 ...
- 3R - 单词数
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...
- 201621123002《Java程序设计》第七周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 事件源,事件处理方法,事件监听器 事件源(Event ...
- python基础之Day9
一.文件操作 1.r+t:可读可写 2.w+t:可写可读 3.a+t:可追加写.可读 4.f.seek(offset,whence) offset代表文件的指针的偏移量,单位是字节byteswhenc ...
- TCP编程
Socket是网络编程的一个抽象概念,通常我们用一个Socket表示“打开了一个网络连接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,在指定协议类型即可. 客户端 大多数连接就是考的 ...
- Linux学习笔记:常用软件
Linux系统下常用软件(针对CentOS,其他系统类似) lrzsz 可用于上传和下载,安装 yum -y install lrzsz ,使用 上传 rz 下载 sz mysql 安装 yum -y ...
- JAVA中内部类(匿名内部类)访问的局部变量为什么要用final修饰?
本文主要记录:在JAVA中,(局部)内部类访问某个局部变量,为什么这个局部变量一定需要用final 关键字修饰? 首先,什么是局部变量?这里的局部是:在方法里面定义的变量. 因此,内部类能够访问某局部 ...
- solr7.7.0搜索引擎使用(二)(添加搜索)
一.安装完毕之后,需要为solr添加core,每一个搜索server就是一个core,solr可以有很多core,我们需要创建一个core用于我们的搜索 添加core的方式有两种: 第一种进入solr ...