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

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

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

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

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

  3. No.008 String to Integer (atoi)

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

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

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

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

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

  6. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

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

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

  9. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

随机推荐

  1. 查看oracle的执行计划

    基于ORACLE的应用系统很多性能问题,是由应用系统SQL性能低劣引起的,所以,SQL的性能优化很重要,分析与优化SQL的性能我们一般通过查看该SQL的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  2. D. Kilani and the Game(多源BFS)

    题目来源:http://codeforces.com/contest/1105/problem/D 题意:编号为1-k的点在一张n*m的表格中依次扩散,每个结点有各自的扩散速度且只可以往上下左右四个方 ...

  3. swift 警告框 - 自定义按钮颜色,图片

    1.封装 弹框http://www.hangge.com/blog/cache/detail_651.html import UIKit extension UIAlertController { / ...

  4. CSS的六大选择器

    选择器:选择器是一种模式,用于选择需要添加样式的元素. 首先简述六大选择器 基本选择器 标签选择器 类选择器 ID选择器 高级选择器 层次选择器 结构伪类选择器 属性选择器 其中基本选择器与层次选择器 ...

  5. Scanner 随机数

    import java.util.Scanner;                                               import java.util.Scanner; Sc ...

  6. rabbitmq shovel插件

    官网说明https://www.rabbitmq.com/shovel.html#management-status 启用shovel插件命令: rabbitmq-plugins enable rab ...

  7. idea连接操作数据库

    场景 本文主要以DB2作为演示,其他数据库大同小异: 网上有很多推荐DB2的连接软件工具,但是因为DB2的使用场景不多,这次是在做数据资产管理的数据质量分析时使用到,在做数据交换时要在DB2中建表并同 ...

  8. loadtxt()函数的糟心历程

    原计划:导入一个csv文件,然后算出平均值 import numpy as np c=np.loadtxt('d:\python36\data.csv', delimiter=',', usecols ...

  9. To handling editor letter

    一般崔稿信写法: Dear Editor: Sorry for disturbing you. We’re not sure if it is the right time to contact yo ...

  10. Javascript上传超大文件实例

    因业务需求需要向伺服器上传大于1GB以上的视频文件,其实网上也能找到很多大文件上传的第三方组件,问题是要么用起来相当不方便,总出现一些bug,要么收费太贵(费用几千,甚至还限定使用数量),最终自己开发 ...