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

  暂时只考虑了这么多种吧,代码如下:

  1. public int myAtoi(String str) {
  2. //对null和空串情况进行判断
  3. if(str == null || str.length() == 0){
  4. return 0 ;
  5. }
  6. //截除字符串的前面的空格
  7. char [] arr = str.trim().toCharArray() ;
  8. int temp ;    //乘子,表示正负
  9. int index ;   //表示最高位开始的index
  10. //判断非空有效位的首位的情况
  11. if((arr[0] == '-') && (arr.length > 1)){
  12. temp = -1 ;
  13. index = 1 ;
  14. }else if((arr[0] <= '9') && (arr[0] >= '0')){
  15. temp = 1 ;
  16. index = 0 ;
  17. }else if((arr[0] == '+') && (arr.length > 1)){
  18. temp = 1 ;
  19. index = 1 ;
  20. }else{
  21. return 0 ;
  22. }
  23.  
  24. long res = 0 ;
  25. for(int i = index ; i < arr.length ; i++){
  26. /*判断每一位是否是数字,不是则直接截断已经处理的结果
  27. * 考虑到int类型的最长有效位数是10位,加上符号位11位,这里再附加一位保险位,
  28. * 超过12位的肯定超出了有效范围我们不继续处理后面的,直接截断*/
  29. if((arr[i] <= '9') && (arr[i] >= '0' && i < 13)){
  30. res = res*10 + temp*(arr[i]-'0') ;
  31. }else{
  32. break ;
  33. }
  34. }
  35. //判断输出的结果是否超出int类型的范围
  36. if(res > Integer.MAX_VALUE ){
  37. return Integer.MAX_VALUE ;
  38. }else if(res < Integer.MIN_VALUE){
  39. return Integer.MIN_VALUE ;
  40. }else{
  41. return (int)res ;
  42. }
  43. }

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. keepalive高可用

    Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,Keepalived除了能够管理LVS软 ...

  2. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  3. android 去掉activity的切换动画

    在styles.xml文件中增加样式代码: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActio ...

  4. [快速幂][NOIP2012]转圈游戏

    转圈游戏 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, ...

  5. python 03 字符串详解

    1.制表符 \t str.expandtabs(20) 可相当于表格 2.def   isalpha(self) 判断是否值包含字母(汉字也为真),不包含数字 3.def   isdecimal(se ...

  6. linux 启动tomcat卡很久的问题

    解决办法:打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容: securerandom.source=file:/dev/random 替 ...

  7. centos7 安装搜狗输入法

    1.root权限,卸载 ibus : yum remove ibus 2.加入EPEL源   sudo yum install epel-release 3.添加mosquito-myrepo源 su ...

  8. this guy gonna be a daddy

    时间真快,媳妇儿怀孕了两个月了. 2016年2月10日,在横峰血检后确定媳妇儿怀孕后,我心情激动得要飞起来一样,那时,我在心里告诉自己不是个孩子了,我自己即将成为孩子的父亲.当时只顾着自个儿激动,已经 ...

  9. .gitignore无效解决方案以及git rm和rm的区别

    一. gitignore 先来了解一下gitignore的常用语法 斜杠“/”表示目录, 是否已斜杠开头有很大区别,如 /build 与 build/ 的区别:其中 build/ 表示不管在哪个位置的 ...

  10. ota升级动画背景色修改

    https://wenku.baidu.com/view/0d63ad25192e45361066f549.html https://blog.csdn.net/huangyabin001/artic ...