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 ...
随机推荐
- JMS的常用方法
import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; impor ...
- Spark运行流程概述
Application 指用户编写的Spark应用程序,其中包含了一个Driver功能的代码和分布在集群中多个节点上运行的Executor代码. Driver Spark中的Driver即运行上述Ap ...
- [实变函数]2.5 Cantor 三分集
1 Cantor 三分集的构造: $$\bex P=\cap_{n=1}^\infty F_n. \eex$$ 2 Cantor 三分 ...
- 发现木马C:\windows\system32\FastUserSwitchingCompatibilityex.dll
而且用安全狗还隔离不了
- keil逻辑分析仪的使用:
http://blog.sina.com.cn/s/blog_4e0175750101kt13.html
- 新建的表如果还没有数据,用exp导的时候会忽略
源地址:http://www.07net01.com/2015/07/884873.html
- dual
1. dual 确实是一张表.是一张只有一个字段,一行记录的表. 2.习惯上,我们称之为'伪表'.因为他不存储主题数据.3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.如:s ...
- 在UEFI下安装windows和Ubuntu双系统目前不可行
UEFI是BIOS的升级,未来将取代BIOS,说白了,就是跟BISO差不多的作用.但是目前比较新的主板兼容两种设置就比较坑了,默认是UEFI,UEFI下只能安装win8以上的版本,和linux64位系 ...
- C++学习24 虚析构函数
在C++中,构造函数用于在创建对象时进行初始化工作,不能声明为虚函数.因为在执行构造函数前对象尚未创建完成,虚函数表尚不存在,也没有指向虚函数表的指针,所以此时无法查询虚函数表,也就不知道要调用哪一个 ...
- 【Oracle经典】132个oracle热门精品资料——下载目录
电子书为网友wglzaj精心整理,这批资料下载量好评率都非常高,广受oracle学习者欢迎.文档共整理了12个精品专题和120个热门资料的下载地址,推荐给大家希望大家喜欢. 目录0豆下载地址:http ...