zoukankan      html  css  js  c++  java
  • LeetCode

    String to Integer (atoi)

    2014.2.10 02:23

    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.

    Solution:

      atoi() is a function declared in <stdio.h>, which converts a C-style string to integer. The "Requirements for atoi" has specified every detail you need before starting coding. Actually you shouldn't expect such detailed specification in written text, but from the mouth of the interviewer, when you're facing a real technical interview.

      The next job is to finish the problem with your code being as simple as possible. Perhaps I could do better, but this one below is enough to get an AC from LeetCode OJ.

      Note that the conversion ends when an invalid character is encountered.

      Time complexity is O(n), where n is the length of the string. Space complexity is O(1).

    Accepted code:

     1 // 2CE, 1RE, 1TLE, 2WA, 1AC, never write your code in a hurry.
     2 #include <climits>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8     int atoi(const char *str) {
     9         int i, len;
    10         int flag;
    11         long long int result = 0;
    12         
    13         if (str == NULL) {
    14             return result;
    15         } else if (str[i] == '-') {
    16             flag = -1;
    17             ++i;
    18         } else if (str[i] == '+') {
    19             flag = +1;
    20             ++i;
    21         } else {
    22             flag = +1;
    23         }
    24         
    25         long long int int_min, int_max;
    26         
    27         int_min = INT_MIN;
    28         int_max = INT_MAX;
    29         while (i < len) {
    30             if (s[i] >= '0' && s[i] < '9') {
    31                 result = result * 10 + (s[i] - '0');
    32                 if (flag == -1) {
    33                     if (-result < int_min) {
    34                         // underflow
    35                         return int_min;
    36                     }
    37                 } else {
    38                     if (result > int_max) {
    39                         // overflow
    40                         return int_max;
    41                     }
    42                 }
    43             }
    44             ++i;
    45         }
    46         
    47         return flag * result;
    48     }
    49 };
  • 相关阅读:
    Flink中的广播流之BroadcastStream
    啊。。这是为什么。。 花甜的工作笔记
    我那庞大身躯,脆弱心灵的3250 花甜的工作笔记
    好吧,我承认,我不是一个专一的人。。。 花甜的工作笔记
    沾沾自喜 花甜的工作笔记
    我高调的来啦。。。。 花甜的工作笔记
    今天偶听一词云终端 花甜的工作笔记
    我开始出轨了。 花甜的工作笔记
    软件限制策略。。。。。辛苦中。。 花甜的工作笔记
    推荐系统建构精选文章
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3542206.html
Copyright © 2011-2022 走看看