1. #define INT_MAX 2147483647
  2. #define INT_MIN -2147483648
  3. class Solution {
  4. public:
  5. int atoi(const char *str) {
  6. //string如果能转换成int的话,各位都要是数字,一个例外是第一个字符可以是负号
  7. int n=strlen(str);
  8. int i=0;
  9. int flag=0;//0表示正值,1表示负值
  10. int val=0;
  11. /*
  12. if(str[0]=='-')
  13. {
  14. i=1;
  15. flag=1;
  16. }
  17. else if(str[0]=='+')//1、好吧,我承认刚开始我只记得负号要单独判断,忘记了正号了,~~~~(>_<)~~~~
  18. {
  19. i=1;
  20. }
  21. //2、好吧,我服了,还有空格,我也都没有考虑,空格可能出现的位置:开头,中间,结尾
  22. //3、我彻底服了,空格在开头和结尾还不能够一视同仁呀,因为" -123"~~~~(>_<)~~~~
  23. */
  24. //这个是专门处理开头空格滴
  25. /*
  26. while(i<n)
  27. {
  28. if(str[i]==' ')
  29. ++i;
  30. else if(str[i]=='-')
  31. {
  32. ++i;
  33. flag=1;
  34. break;
  35. }
  36. else if(str[i]=='+')
  37. {
  38. ++i;
  39. break;
  40. }
  41. }
  42. *///一直显示超时,调试才发现这里是个死循环,晕
  43. while(i<n)
  44. {
  45. if(str[i]==' ')
  46. ++i;
  47. else if(str[i]=='-')
  48. {
  49. ++i;
  50. flag=1;
  51. break;
  52. }
  53. else if(str[i]=='+')
  54. {
  55. ++i;
  56. break;
  57. }
  58. else
  59. break;
  60. }
  61. while(i<n)
  62. {
  63. //if(str[i]==' ') //其实字符串中间有空格的话,算是能转为int呀还是不能转为int呀,纠结,我的写法是算是能转了
  64. //++i;//如果用for时,因为后面已经++i了,所以这里应该是空操作,不能再写++i了
  65. //Input:" -0012a42" Output:0 Expected:-12 测试用例的意思是,把能返回的第一串都给返回了
  66. //if(str[i]<'9'&&str[i]>'0') //写错了吧
  67. if(str[i]<='9'&&str[i]>='0')
  68. {
  69. //val=val*10+str[i];
  70. val=val*10+(str[i]-'0');
  71. ++i;//这里用的是while,所以这里要手动++i;
  72. }
  73. else
  74. {
  75. //return 0;//如果字符串不能转换为int的话,返回什么呀,这个很纠结呀,提交的错误用例说明返回值应该是0
  76. break;
  77. }
  78. }
  79. if(flag)
  80. val=-val;
  81. //Input: "2147483648" Expected: 2147483647
  82. // If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
  83. if(flag==0&&val<)//判断是否溢出
  84. return INT_MAX;
  85. else if(flag==1&&val>0)
  86. return INT_MIN;
  87. else
  88. return val;
  89. }
  90. };

刚开始我没有看提示,直接做的,然后就各种错呀

  1. Requirements for atoi:
  2. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
  3.  
  4. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
  5.  
  6. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
  7.  
  8. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.  

Submission Result: Wrong Answer

Input: " 10522545459"
Output: 1932610867
Expected: 2147483647

说明我的溢出判断那个地方写的不对,对于这道题目来说,怎么判断溢出,什么时候判断溢出,我觉得如果在最后判断溢出是不好的,因为输入的是字符串,不能知道它的int值是多少就不能和之后出来的值比较,所以判断溢出应该是在转换的过程中判断的,至于具体怎么做,我想不到,看看别人是怎么写的吧:http://blog.csdn.net/ithomer/article/details/8800530

  1. class Solution {
  2. public:
  3. int atoi(const char *str) {
  4. int i=;
  5. int n=strlen(str);
  6. int flag=;
  7. int value=;
  8. int digit=;
  9. while(*str!='\0')
  10. {
  11. if(*str==' ')
  12. {
  13. str++;
  14. }
  15. else if(*str=='-')
  16. {
  17. flag=;
  18. str++;
  19. break;
  20. }
  21. else if(*str=='+')
  22. {
  23. str++;
  24. break;
  25. }
  26. else
  27. {
  28. break;
  29. }
  30. }
  31. while(*str!='\0')
  32. {
  33. if(*str<=''&&*str>='')
  34. {
  35. digit=*str-'';
  36. //要满INT_MIN<=val*10+digit<=INT_MAX
  37. if(flag==&&value>(INT_MAX-digit)/)
  38. return INT_MAX;
  39. //else if(value<(INT_MIN-digit)/10)//负值不能这么算
  40. else if(flag==&&value>(-INT_MIN-digit)/)
  41. return INT_MIN;
  42. else
  43. {
  44. value=value*+digit;
  45. str++;
  46. }
  47. }
  48. else
  49. {
  50. break;
  51. }
  52. }
  53. if(flag)
  54. value=-value;
  55. return value;
  56. }
  57. };

Submission Result: Wrong Answer

Input: " -00134"
Output: -2147483648
Expected: -134

不明真相,我在codeblocks中跑了代码,输入" -00134",输出的就是-134,不知道到底是怎么回事

String to Integer (atoi) ???的更多相关文章

  1. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  2. 【leetcode】String to Integer (atoi)

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

  3. No.008 String to Integer (atoi)

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

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

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

  5. 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 ...

  6. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

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

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

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

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

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

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

随机推荐

  1. C语言小知识点汇集

    1. 三元表达式“?:”冒号两边的两个操作数类型必须一致. 2. 枚举变量的sizeof值同int型一样为4. 3. 为了消除不必要的计算 &&两侧的表达式,当左侧即第1个表达式不成立 ...

  2. WinForm---进度条的实现方法

    (转自:http://www.cnblogs.com/Sue_/articles/2024932.html) 看了好几个WinForm程序了,发现他们对进度条的处理完全失去了进度条的作用.他们都是采用 ...

  3. Makefile的补充学习

    通配符%和Makefile自动推导(规则)(1)%是Makefile中的通配符,代表一个或几个字母.也就是说%.o就代表所有以.o为结尾的文件.(2)所谓自动推导其实就是Makefile的规则.当Ma ...

  4. hdu1845

    题解: 只要输出n/2即可 代码: #include<cstdio> #include<cmath> #include<cstring> #include<a ...

  5. YYMMDD转YYYY-MM-DD

    date.replace(/(\d{4})(\d{2})(\d{2})/g,'$1-$2-$3');

  6. Python基础学习(第5天)

    第3课  模块 1.模块(module) Python中一个.py文件就是一个模块,可以调用其它文件中的程序. 例:first.py def laugh(): print '哈哈哈哈哈' second ...

  7. 20165210 Java第四次实验报告

    20165210 实验四 Android程序设计 实验步骤 第24章:初识Android 任务一:完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号 学 ...

  8. vsftpd的530 Login incorrect错误解决方法 vsftpd登录错误

    530 Login incorrect只有用匿名anonymous才可登录,其余所有用户都报530 Login incorrect错 复制代码 代码如下: local_enable=YESwrite_ ...

  9. PostBack IsPostBack

    这涉及到aspx的页面回传机制的基础知识 postback是回传 即页面在首次加载后向服务器提交数据,然后服务器把处理好的数据传递到客户端并显示出来,就叫postback, ispostback只是一 ...

  10. Arcgis for JS扩展GraphicLayer实现区域对象的聚类统计与展示

    功能需求: 分省市统计并展示全国雨量站的数目与位置. 常规做法: 分省市雨量站的数目通过统计表的形式在页面端展示,位置根据XY坐标信息将雨量站标绘在图上. 优化做法: 去掉统计图的展示方式,直接将各省 ...