zoukankan      html  css  js  c++  java
  • 19.1.25 [LeetCode8]String to Integer (atoi)

    Implement atoi which converts a string to an integer.

    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.

    Note:

    • Only the space character ' ' is considered as whitespace character.
    • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

    Example 1:

    Input: "42"
    Output: 42
    

    Example 2:

    Input: "   -42"
    Output: -42
    Explanation: The first non-whitespace character is '-', which is the minus sign.
                 Then take as many numerical digits as possible, which gets 42.
    

    Example 3:

    Input: "4193 with words"
    Output: 4193
    Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
    

    Example 4:

    Input: "words and 987"
    Output: 0
    Explanation: The first non-whitespace character is 'w', which is not a numerical 
                 digit or a +/- sign. Therefore no valid conversion could be performed.

    Example 5:

    Input: "-91283472332"
    Output: -2147483648
    Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
                 Thefore INT_MIN (−231) is returned.
     1 class Solution {
     2 public:
     3     int myAtoi(string str) {
     4         long long ans = 0;
     5         bool flag = false, plus = true;
     6         int s = 0;
     7         for (int i = 0; str[i]; i++) {
     8             if (str[i] <= '9'&&str[i] >= '0'|| str[i] == '-'|| str[i] == '+') {
     9                 if (str[i] == '-')
    10                     plus = false;
    11                 if (str[i] == '+')
    12                     plus = true;
    13                 if (str[i] <= '9'&&str[i] >= '0')
    14                     s = i;
    15                 else
    16                     s = i + 1;
    17                 flag = true;
    18                 break;
    19             }
    20             if (str[i] != ' ')
    21                 break;
    22         }
    23         if (!flag)
    24             return 0;
    25         for (int i = s; str[i] <= '9' && str[i] >= '0'; i++) {
    26             ans *= 10;
    27             if(plus)
    28                 ans += str[i] - '0';
    29             else
    30                 ans -= str[i] - '0';
    31             if (ans > INT_MAX || ans < INT_MIN)
    32                 break;
    33         }
    34         if (ans > INT_MAX)
    35             return INT_MAX;
    36         if (ans < INT_MIN)
    37             return INT_MIN;
    38         return ans;
    39     }
    40 };
    View Code

    注意一下特殊情况的考虑,不小心的话就会WA

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    对话框中获取Month Calendar的当前日期
    JAVA GUI程序示例
    MFC控件显示值和控件变量值的更新
    JAVA求10到99的随机数
    学习之路二:关于集合和数组内在联系的深入了解
    迭代器学习之三:IEnumerable和IEnumerator的泛型结构
    迭代器学习之四:关于yield的深入了解
    迭代器学习之二:数组的可枚举类型和枚举数的定义以及编译器的foreach工作原理
    学习之路三:关于运用单线程和委托以及事件自定义Timer类
    迭代器学习之一:使用IEnumerable和IEnumerator接口
  • 原文地址:https://www.cnblogs.com/yalphait/p/10318403.html
Copyright © 2011-2022 走看看