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)的更多相关文章

  1. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  2. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

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

  3. 【LeetCode】008. String to Integer (atoi)

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

  4. [Leetcode]008.String to Integer (atoi)

    public class Solution { public int myAtoi(String str) { int index = 0, sign = 1, total = 0; //1. 边界条 ...

  5. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

  6. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【leetcode】String to Integer (atoi)

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

  8. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  9. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

随机推荐

  1. 【转】一道SQL SERVER DateTime的试题

    学习过上一篇SQL SERVER DateTime精度的文章后.再来做一道题. IF ('2011-07-31 00:00:00.000' BETWEEN '2011-07-01' and '2011 ...

  2. python3读取文件

    #coding:utf-8 rfile = open('test.txt','r') str=[] for x in rfile: str = x.split(',') for x in str: p ...

  3. Sprite的原点

    对于某些类型的游戏,例如飞行射击游戏或者顶视角的2D游戏,通常会直接选取图片的中心做为原点.但对于<冰杖秘闻>这样的 2D ARPG 来说,会更多的从三维场景的角度来选择原点,即选择物品底 ...

  4. room_speed和image_speed

    room_speed是游戏步数,每秒多少步(步事件)image_speed是动画帧率room_speed变则整个游戏变慢image_speed变只是该object动画变慢 除了游戏全局加速减速,一般不 ...

  5. VC++、MFC、COM和ATL的区别

    今天看到的,感觉不错.转载了 一.什么是MFC 微软基础类(Microsoft Foundation Classes),实际上是微软提供的,用于在C++环境下编写应用程序的一个框架和引擎,VC++是W ...

  6. studio--问题

    2,Android studio 怎么能删除干净一个module? 工程→open module settings→选中你要删除的module,然后点“一”,确定后返回,即可在module邮件下看到d ...

  7. listview当选中某一个item时设置背景色其他的不变

    listview当选中某一个item时设置背景色其他的不变: 可以使用listview.setOnFoucsChangeListener(listener) ; /** * listview获得焦点和 ...

  8. JAVA 静态成员 static

    static关键字 一.作用:是一个修饰符,用于修饰成员(成员变量,成员方法)1.被static 修饰后的成员变量只有一份2.当成员被static修饰之后,多了一种访问方式,除了可以被对象调用之外还可 ...

  9. Java注解处理器--annotation学习四

    Java中的注解(Annotation)是一个很神奇的东西,特别现在有很多Android库都是使用注解的方式来实现的.一直想详细了解一下其中的原理.很有幸阅读到一篇详细解释编写注解处理器的文章.本文的 ...

  10. Android开发者需要面对的8大挑战

    移动开发变得越来越受欢迎,但移动开发者正面临着一系列挑战.本文将介绍的是Android开发者需要面对的8个不利因素,例如缺乏硬件标准化,以及软件碎片.为Android OS开发app,给予了开发人员极 ...