非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面。

题目:将一个string转换为int。实现函数atoi()的功能。

先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了)

input output
”+1“ 1
”   +   1“  0(error了)
”       1“ 1(前头只有空格是合法的)
”12b45“ 12(取前面的数字)
溢出 : ”2147483648“(负数情况同) 2147483647(MAX_VALUE)

类似我对atoi()的功能不熟的人来说,这道题就是不停WA的血泪史。每次要修正一次错误答案。

最终AC:

  1. public int atoi(String str) {
  2. int len = str.length();
  3. long num = 0;
  4. //用long型存储,以处理溢出的情况
  5. int ifNegative = 1;
  6. boolean numStatus = false;
  7. for(int i = 0 ; i < len ; i++){
  8. char ch = str.charAt(i);
  9. if(numStatus && (ch < '0' || ch > '9')) break;
  10. else if(numStatus && ch >= '0' && ch <= '9'){
  11. num = num * 10 + (ch - '0');
  12. }else if(!numStatus && ch != '-' && ch != '+' && (ch < '0' || ch > '9')){
  13. num = 0;
  14. break;
  15. }else if(!numStatus && ch == '-'){
  16. numStatus = true;
  17. ifNegative = -1;
  18. }else if(!numStatus && ch == '+'){
  19. numStatus = true;
  20. }else if(!numStatus && ch >= '0' && ch <= '9'){
  21. numStatus = true;
  22. num = num * 10 + (ch - '0');
  23. }
  24.  
  25. }
  26. num *= ifNegative;
  27.  
  28. int result = 0;
  29. if(num > Integer.MAX_VALUE) result = Integer.MAX_VALUE;
  30. else if(num < Integer.MIN_VALUE) result = Integer.MIN_VALUE;
  31. else result = (int) num;
  32.  
  33. return result;
  34.  
  35. }

[leetcode]_String to Integer (atoi)的更多相关文章

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

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

  2. [LeetCode] String to Integer (atoi) 字符串转为整数

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

  3. LeetCode OJ-- String to Integer (atoi) **

    https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...

  4. [Leetcode] String to integer atoi 字符串转换成整数

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

  5. [LeetCode] String to Integer (atoi) 字符串

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

  6. [LeetCode]-algorithms-String to Integer (atoi)

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

  7. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  8. [LeetCode]String to Integer (atoi)

    题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...

  9. leetcode String to Integer (atoi) python

    class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int "& ...

随机推荐

  1. [ActionScript 3.0] AS3调用百度地图API

    package { import baidu.map.basetype.LngLat; import baidu.map.basetype.Size; import baidu.map.config. ...

  2. 视频最后用使用了function(i,ot)一笔带过,但我看不懂i和ot这2个参数的具体值是怎么获取得到的,能不能说一下参数传递过程?

    使用函数设置文本内容: $(selector).text(function(index,currentcontent)) 参数描述content必需.规定被选元素的新文本内容.注意:  特殊字符会被编 ...

  3. viewPager双层嵌套的事件问题

    问题描述:  ViewPager嵌套viewPager, 当childViewPager中加入了Onclick事件, 导致childViewpager的滑动消息被拦截掉. 很无奈. 解决方法: 重写c ...

  4. OpenStack学习

    ========================================== openstack的场景是什么 openstack的目的是把空闲的机器组织起来,经过虚拟化,租给用户使用. 出现的 ...

  5. cocos2d3.0跑酷代码讲解和源码

    最近在网上看到一个跑酷代码的例子,写的很不错,连接如下 http://www.waitingfy.com/archives/783 现在我把他精简了一下,去除了一些比较简单的特效,着重保留了主角的跳跃 ...

  6. python学习(二):python基本语法

    前言:python基本的语法与其他语言诸如C,JAVA等类似,但个中有些许不同. 一.常规语法 1.变量名与关键字 与其他语言类似,变量名由字母.数字.下划线组成,且必须由字母开头. 变量使用不需要提 ...

  7. 搭建Artifactory集群

    搭建Artifactory集群 制品仓库系统有很多,例如Artifactory.Archiva.Sonatype Nexus.Eclipse Package Drone,其中Artifactory拥有 ...

  8. 学习练习 java20160507作业

    第一题 求水仙花的个数: //求水仙花数 int zongshu = 0; for(int i =100; i<=999;i++) { int bai = i/100; //求百位上面的数字 i ...

  9. noip2007 树网的核

    P1099 树网的核 112通过 221提交 题目提供者该用户不存在 标签动态规划树形结构2007NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录   题目描述 设T=(V, E, W) ...

  10. 初识Ruby

    以下为看<七周七语言>的第一课,找到答案,参考资料来源于http://book.douban.com/annotation/27705657/ Ruby API文档在这里http://ru ...