zoukankan      html  css  js  c++  java
  • String to Integer (atoi)

    1. Question

    将字符串转换为整数,考虑各种输入情况:

    • 空格处理:开头空格省略
    • 有效数字:从第一个非空格字符开始的是+、-或数字,直到下一个非数字字符结束。
    • 加号处理:开头加号省略
    • 空串处理
    • 溢出处理
    • 无效数字处理
    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.
    
    spoilers alert... click to show requirements for atoi.
    
    Requirements for atoi:
    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.
    
    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.
    
    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.
    
    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.

    2. Solution

    考虑上述所有情况即可。

     1 public class Solution {
     2     
     3     public int atoi(String s) {
     4         int i = 0;
     5         int len = s.length();
     6         for( ;i<len && s.charAt(i) == ' '; i++ );
     7         long res = 0;
     8         int flag =1;
     9         if( i<len ){
    10             char c = s.charAt(i);
    11             if( c == '-' ){
    12                 flag = -1;
    13                 i++;
    14             }
    15             if( c == '+' )
    16                 i++;
    17             for( ; i<len; i++ ){
    18                 c = s.charAt(i);
    19                 if( c<'0' || c>'9' )
    20                     break;
    21                 res = res * 10 + c - '0';
    22                 if( res*flag > Integer.MAX_VALUE )
    23                     return Integer.MAX_VALUE;
    24                 if( res*flag < Integer.MIN_VALUE )
    25                     return Integer.MIN_VALUE;
    26             }
    27         }
    28         return (int)res * flag;
    29     }
    30 }
    View Code

    3. 复杂度

    O(n)

  • 相关阅读:
    【P1133】 教主的花园
    【P1095】 守望者的逃离
    【P1063】 能量项链
    【模板】st表
    【模板】单源最短路径——Floyd
    jQuery的XX如何实现?——4.类型检查
    jQuery的XX如何实现?——3.data与cache机制
    jQuery的XX如何实现?——2.show与链式调用
    jQuery的XX如何实现?——1.框架
    《拖延症再见》
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4596306.html
Copyright © 2011-2022 走看看