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

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    (数据看成字符串)大端——高尾端,小端——低尾端链接
    ASP.NET 页生命周期概述
    asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解
    Server.MapPath()目录详解
    C# ASP.NET FILEUPLOAD详解
    HTML中id、name、class 区别
    注册表新建 -------DWORD(32-位)
    XSD标准架构-----<xsd:element> 元素详解
    html5学习链接
    关于HSL和HSV颜色空间的详细论述
  • 原文地址:https://www.cnblogs.com/yalphait/p/10318403.html
Copyright © 2011-2022 走看看