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 };
  • 相关阅读:
    proxmox新版本使用了lxc容器,导致以前的vzlist命令无法使用,于是自己写了一个脚本来获取所有半虚拟化主机的信息状态
    Linux 系统优化参数总结
    linux shell 远程执行命令
    wios设置证书登陆
    Eclipse安装tomcat插件
    Centos6.5-dnsmasq安装
    SSL工作原理
    ssh免密码登陆及其原理
    Linux查看后台任务,关闭后台任务
    Mybatis 中常用的java类型与jdbc类型
  • 原文地址:https://www.cnblogs.com/panda_lin/p/string_to_integer_atoi.html
Copyright © 2011-2022 走看看