zoukankan      html  css  js  c++  java
  • LeetCode 8. String to Integer (atoi) (字符串到整数)

    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.

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.


    题目标签:String

      题目给了我们一个 str,让我们把它 转换为 int。

      其中有很多违规的条件没有说明:

        正负的符号只能有0个 或者 1个;

        符号后面就应该是数字了,如果遇到不是数字的符号,返回目前为止合格的数字,不需要考虑后面的数字;

        如果数字overflow,大于MAX的要返回MAX,小于MIN 的要返回MIN;

        etc。

    Java Solution:

    Runtime beats 57.67% 

    完成日期:01/09/2017

    关键词:String

    关键点:考虑到所有违规情况

     1 class Solution 
     2 {
     3     public int myAtoi(String str) 
     4     {
     5         long res = 0;        // the res number to return. Note: res to return should be long and cast it to int when return it at the end.
     6         int sign = 1;        // the sign before the number. default is 1 (positive).
     7         int index = 0;        // index for num string to go through.
     8 
     9         
    10         // Step 0: if parameter str is null or "", then return 0.
    11         if(str.length() == 0 || str == null)
    12             return 0;
    13         
    14         // Step 1: trim the whitespace.
    15         str = str.trim();
    16         
    17         // Step 2: check first char is '+' or '-', move the index by 1 and also sign value.
    18         if(str.charAt(0) == '+')
    19             index++;
    20         else if(str.charAt(0) == '-')    
    21         {
    22             index++;
    23             sign = -1;    // change the sign to -1 (negative).
    24         }
    25         
    26         // Step 3: go through the str string.
    27         for(; index<str.length(); index++)
    28         {
    29             // if this char is not a number char, then break. No matter there are more numbers after.
    30             if(str.charAt(index) > '9' || str.charAt(index) < '0')
    31                 break;
    32             
    33             // add this char value into res.
    34             res = res * 10 + (str.charAt(index) - '0');        // char - '0' is the correct int value.
    35             
    36             // check the num exceed the max or not.
    37             if(res > Integer.MAX_VALUE)        // res should be long because here res might be over Integer.max value.
    38                 break;
    39         }
    40         
    41         
    42 
    43         // Step 4: depending on the sign and max or min value, return res.
    44         if(res * sign >= Integer.MAX_VALUE)
    45             return Integer.MAX_VALUE;
    46         else if(res * sign <= Integer.MIN_VALUE)
    47             return Integer.MIN_VALUE;
    48         
    49         // goes here meaning the res number doesn't exceed max and min integer value.
    50         return (int)res * sign;        // here need to cast res to int.
    51     }
    52 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    微软发布TFS 2018!
    Git小技巧:VIM中如何填写注释信息
    为某金融企业的IT技术部人员提供基于TFS的软件研发流程介绍
    【TFS 2017】使用浏览器上传文件(TFVC)或者编辑代码,错误提示TF14098,需要对文件有PendChange 权限
    TFS 2017 培训
    签入代码(新建分支,新建推拉请求)关联工作项,却找不到自己需要的工作项
    TFS支持移动设备,微软已经走出了第一步(手机上更新、查询工作项)
    在TFS持续集成(持续发布)中执行Telnet任务
    Nginx
    单点登录
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/8016169.html
Copyright © 2011-2022 走看看