zoukankan      html  css  js  c++  java
  • [Leetcode 15] 8 String to Integer (atoi)

    Problem:

    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.

    Analysis:

    Possible input cases:

    1. "", empty string

    2. "123", simple valid string

    3. "+123", valid string with '+' sign

    4. "-123", valid string with '-' sign

    5. " 123abc ", string with space and other characters

    6. "abc123", invalid input string

    7. "33333333333333", "-3333333333333", invalid overflow string

    According to those given cases, we should follow the steps listed below:

    1. check whether the string is empty, define what to return if the string is empty; if it's empty, return directly

    2. trim the spaces of the string both at the head and the end

    3. judge whether the first character is '+' or '-', by default no either of them are positive num

    4. start from the first position, continue convert until meeting the end of the string or a non-numeric character

    5. judge whether the result overflows, if overflow, return the MAX_VALUE or MIN_VALUE, else return the result

    Code:

    View Code
     1 public class Solution {
     2     public int atoi(String str) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5             if (str.equals("")) return 0;
     6         
     7         int pos = 0, res = 0;
     8         long val = 0L;
     9         char flag = '+';
    10         
    11         str = str.trim();
    12         
    13         if (str.charAt(0)=='+' || str.charAt(0)=='-') {
    14             pos++;
    15             flag = str.charAt(0);
    16         }
    17         
    18         while (pos < str.length() && str.charAt(pos)>='0' 
    19                 && str.charAt(pos)<='9') {
    20             val = val * 10 + (str.charAt(pos)-'0');
    21             pos++;
    22         }
    23         
    24         if (flag == '-')
    25             val = -val;
    26         
    27         if (val > Integer.MAX_VALUE)
    28             res = Integer.MAX_VALUE;
    29         else if (val < Integer.MIN_VALUE)
    30             res = Integer.MIN_VALUE;
    31         else
    32             res = (int) val;
    33         
    34         return res;
    35     }
    36 }

    Attention:

    To test whether str is "", please use str.equals(""), not str == "". Remember, all variable not of primitive type is reference in java! 

    The overflow problem is also another very interesting problem, to test overflow, we have to use long to store the intermediate result, then test its validity.

    Remember to test the positive or negative of the number

  • 相关阅读:
    Serverless 架构到底要不要服务器?
    Serverless 在 SaaS 领域的最佳实践
    20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
    创业公司用 Serverless,到底香不香?
    如何通过 Serverless 轻松识别验证码?
    Serverless 是一种思想状态
    都 2021 年了,Serverless 能取代微服务吗?
    从零入门 Serverless | 架构的演进
    Serverless 的价值
    实验楼前后端代码
  • 原文地址:https://www.cnblogs.com/freeneng/p/3022107.html
Copyright © 2011-2022 走看看