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 };
  • 相关阅读:
    BZOJ4802 欧拉函数 数论
    BZOJ3561 DZY Loves Math VI 数论 快速幂 莫比乌斯反演
    BZOJ3560 DZY Loves Math V 数论 快速幂
    BZOJ2142 礼物 扩展lucas 快速幂 数论
    BZOJ1951 [Sdoi2010]古代猪文 中国剩余定理 快速幂 数论
    BZOJ1500 [NOI2005]维修数列 splay
    HDU1814 Peaceful Commission 2-sat
    BZOJ2209 [Jsoi2011]括号序列 splay
    BZOJ1503 [NOI2004]郁闷的出纳员 splay
    BZOJ1208 [HNOI2004]宠物收养所 splay
  • 原文地址:https://www.cnblogs.com/panda_lin/p/string_to_integer_atoi.html
Copyright © 2011-2022 走看看