• 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)

  • 相关阅读:
    [三]JavaIO之IO体系类整体设计思路 流的概念以及四大基础分类
    [二] JavaIO之File详解 以及FileSystem WinNTFileSystem简介
    [一]FileDescriptor文件描述符 标准输入输出错误 文件描述符
    [零] JavaIO入门简介 程序设计语言 为什么需要IO库
    装饰器模式 Decorator 结构型 设计模式 (十)
    适配器模式 adapter 结构型 设计模式(九)
    layui 鼠标悬停单元格显示全部
    mysql 日期总结
    区域块路由与全局路由兼容,双重路由
    JS-SDK相关参考
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4596306.html
Copyright © 2020-2023  润新知