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

  • 相关阅读:
    Django模型层Meta内部类详解
    jquery checkbox的相关操作——全选、反选、获得所有选中的checkbox
    c# 委托与异步调用
    DataTable转成List集合
    c# winform 自动升级
    C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常
    C#创建无窗体的应用程序
    sql 一个表的字段更新至另一个字段的方法
    datagridview 获取选中行的索引
    CHECKEDLISTBOX用法总结
  • 原文地址:https://www.cnblogs.com/freeneng/p/3022107.html
Copyright © 2011-2022 走看看