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

    http://oj.leetcode.com/problems/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.
    
    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.

    思路

    处理好以下几种情况就OK了:

    1. 正负数。
    2. 无效字符。
    3. 溢出。简单起见使用unsigned long long,这样判断溢出比较方便。
     1 class Solution {
     2 public:
     3     int atoi(const char *str) {
     4         const char *p = str;
     5         bool minus = false;
     6         
     7         while (' ' == *p) {
     8             ++p;
     9         }
    10         
    11         if (('+' == *p) || ('-' == *p)) {
    12             if ('-' == *p) {
    13                 minus = true;
    14             }
    15             
    16             ++p;
    17         }
    18         
    19         if (('' == *p) || (*p < '0') || (*p > '9')) {
    20             return 0;
    21         }
    22         
    23         unsigned long long ull = 0;
    24         
    25         while (*p != '') {
    26             if ((*p < '0') || (*p > '9')) {
    27                 break;
    28             }
    29             
    30             ull = ull * 10 + (*p - '0');
    31             
    32             if (minus) {
    33                 if (ull > 0x80000000) {
    34                     return INT_MIN;
    35                 }
    36             }
    37             else {
    38                 if (ull > 0x7fffffff) {
    39                     return INT_MAX;
    40                 }
    41             }
    42             
    43             ++p;
    44         }
    45         
    46         return minus ? -1 * (int)ull : (int)ull;
    47     }
    48 };
  • 相关阅读:
    FastJson序列化枚举类
    优雅计时StopWatch
    spark算子之aggregateByKey
    scala系列列表
    iOS开发证书不受信任
    .NET Core 中的鉴权授权正确方式
    设置双击直接打开.ipynb文件
    Ubuntu18添加开机启动项
    iOS ObjectC 笔记(二)GCD
    iOS VideoToolBox decoder解码失败(12909和12911)问题解决(二)
  • 原文地址:https://www.cnblogs.com/panda_lin/p/string_to_integer_atoi.html
Copyright © 2011-2022 走看看