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 };
  • 相关阅读:
    React 获取服务器API接口数据:axios、fetchJsonp
    nvm管理node版本
    windows自定义命令的创建
    目标平台、活动平台 配置,出现未能加载文件或程序集“xxx”或它的某一个依赖项报错
    Quartz.net使用总结
    vs 2010 中类文文件模板的修改
    js获取url参数的两种方法
    遍历文件夹
    简单多条件动态查询的实现
    ajax请求跨域问题
  • 原文地址:https://www.cnblogs.com/panda_lin/p/string_to_integer_atoi.html
Copyright © 2011-2022 走看看