• 8. String to Integer (atoi)


    1. 问题描述

    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.

    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.

    Tags: Math String

    Similar Problems: (E) Reverse Integer (H) Valid Number

    2. 解答思路

    3. 代码

      1 #include <string>
      2 #include <cctype>
      3 using namespace std;
      4 class Solution {
      5 public:
      6     int myAtoi(string str) {
      7         m_gIsValidate = Validate(str);
      8         if (m_gIsValidate)
      9         {
     10             int sum = 0;
     11             bool bIsPositive = true;
     12             int nOper = 0;
     13             bool bIsDigitFind = false;
     14             for (size_t i=0, n=0; i<str.size(); i++)//n用于记录当前sum的位数
     15             {
     16                 if (str[i]=='-' || str[i]=='+')
     17                 {
     18                     if ('-' == str[i])
     19                     {
     20                         bIsPositive = false;
     21                     }    
     22                     nOper++;
     23                     if (nOper > 1)
     24                     {
     25                         return 0;
     26                     }
     27                 }
     28                 else if (isdigit(str[i]))
     29                 {                    
     30                     bIsDigitFind = true;
     31                     if (sum > 214748364 && 9 == n)
     32                     {
     33                         return bIsPositive ? 2147483647 : -2147483648; 
     34                     }
     35                     else if (sum == 214748364 && 9 == n)
     36                     {
     37                         if (str[i] - '0' > 7)
     38                         {
     39                             return bIsPositive ? 2147483647 : -2147483648;
     40                         }
     41                         sum = sum * 10 + (str[i] - '0');
     42                     }
     43                     else
     44                     {                        
     45                         if (10 == n)
     46                         {
     47                             return bIsPositive ? 2147483647 : -2147483648;
     48                         }
     49                         sum = sum * 10 + (str[i] - '0');
     50                     }
     51                     n++;
     52                 }
     53                 else if (isspace(str[i]))
     54                 {
     55                     if (bIsDigitFind)
     56                     {
     57                         return bIsPositive ? sum : -sum;
     58                     }
     59                     else
     60                     {
     61                         continue;
     62                     }
     63                 }
     64                 else if (isalpha(str[i]))
     65                 {
     66                     return bIsPositive ? sum : -sum;
     67                 }
     68 
     69             }
     70             return bIsPositive ? sum : -sum;
     71         }
     72         return 0;
     73 
     74     }
     75 private:
     76     bool Validate(string &str)
     77     {
     78         if (0 == str.size())
     79         {
     80             return false;
     81         }
     82         bool bIsUnSpaceFind = false;
     83         bool bIsDigitFind = false;
     84         for (size_t i=0; i<str.size(); i++)
     85         {
     86             if (str[i] == '+' || str[i] == '-')
     87             {
     88                 bIsUnSpaceFind = true;
     89                 if (bIsDigitFind)
     90                 {
     91                     return false;
     92                 }
     93                 continue;
     94             }
     95             if (isspace(str[i]))
     96             {
     97                 if (bIsUnSpaceFind)
     98                 {
     99                     return false;
    100                 }
    101                 continue;
    102             }
    103             if (isdigit(str[i]) || isalpha(str[i]))
    104             {
    105                 bIsDigitFind = true;
    106             }
    107             else
    108             {
    109                 return false;
    110             }
    111         }
    112         return true;
    113     }
    114 
    115     bool m_gIsValidate;
    116 };

    4. 反思

    • 需要考虑正负号 "+1"
    • " 010"
    • " +004500"
    • "+-2"
    • " -0012a42"
    • " +0 123"
    • " -0012a42"
    • "2147483648"
    • "123 456"
    • " - 321"
    • " -11919730356x"
  • 相关阅读:
    cocostudio 使用教程
    anrdroid AVD启动不起来的问题。Waiting for HOME ('android.process.acore') to be launched
    Android SDK无法更新的问题解决办法
    Code(容斥,好题)
    莫比乌斯反演(转)
    随笔--新建查询
    11427
    uva11722
    uva11021
    How many integers can you find(容斥+dfs容斥)
  • 原文地址:https://www.cnblogs.com/whl2012/p/5581945.html
Copyright © 2020-2023  润新知