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. 二叉搜索树与双向链表(python)

    题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. # -*- coding:utf-8 -*- # class TreeNo ...

  2. poj2115(扩展欧基里德定理)

    题目链接:https://vjudge.net/problem/POJ-2115 题意:模拟for循环for(int i=A;i!=B;i+=C),且数据范围为k位无符号数以内,即0~1<< ...

  3. sed (未完,待续)

    sed [options] 'command' file(s) options: -e<script>或--expression=<script>:以选项中的指定的script ...

  4. sqlserver2017 重装过程中出现“无法找到数据库引擎启动句柄”错误的解决办法

    sqlserver数据库引擎修改账号名,详情参考:http://blog.51cto.com/djclouds/2089047?utm_source=oschina-app 在SQL Server安装 ...

  5. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  6. [leetcode]31. Next Permutation下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 虚拟机下 centos7 无法连接网络

    [root@localhost ~]# cd /etc/sysconfig/network-scripts [root@localhost network-scripts]# ls ifcfg-ens ...

  8. JVM思考-ClassLoader.loadClasshe和Class.forName区别

    JVM思考-ClassLoader.loadClasshe和Class.forName区别 目录:JVM总括:目录 见博客第四节:JVM总括四-类加载过程.双亲委派模型.对象实例化过程

  9. python 之 函数

    什么是函数 引言 现在有这么个情况:假设我们python中的len方法不可以使用了,而恰好你又要计算一个字符串的长度你该怎么办呢?有人说:‘简单,可以使用for循环嘛 s1 = "hello ...

  10. Python3基础知识之数据结构List和Tuple

    问题:今天学习python数据结构中的List和Tuple. 目标:了解二者的区别,学会一般的应用 相关知识:列表(List) : 类似于 .NET ArrayList / List.元组(Tuple ...