zoukankan      html  css  js  c++  java
  • 8. String to Integer (atoi)

    description:

    Implement atoi which converts a string to an integer.
    实现string转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.
    这个功能首先要去掉在string前面的空格,如果第一个非空的字符是正负号,则要做一个标记,在他的后边要找到尽可能多的数字,然后都转成整形类型。

    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.
    如果第一个字符既不是正负号也不是数字就stop

    If no valid conversion could be performed, a zero value 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.
    

    my answer:

    感恩

    class Solution {
    public:
        int myAtoi(string str) {
            if (str.empty()) return 0; //要考虑string为空的情况
            int base = 0, sign = 1, i = 0, n = str.size();
            while(i < n && str[i] == ' ') ++i; // i<n must always to think about
            if((str[i] == '+' ||str[i] == '-') && i < n){
                sign = (str[i++] == '+') ? 1 : -1; 
                //cause the i in while and not in for ,but it must to add every iteration, so use the i++ to imply the add-one function
            }
            while(i < n && str[i] >= '0' && str[i] <= '9'){
                if(base > INT_MAX/10 || (base == INT_MAX/10 && str[i] - '0' > 7)){
                    return (sign == 1) ? INT_MAX : INT_MIN;
                }
                base = base * 10 + (str[i++] - '0');
            }
            return sign * base;
        }
    };
    

    relative point get√:

    在while循环里还想实现for循环的i++,可以在while的代码段里调用i的时候就用i++,如上面代码里的str[i++]

    hint :

    关键在于数清可能遇到的可能情况

  • 相关阅读:
    next_permutation
    P1087 FBI树
    P4047 [JSOI2010]部落划分
    买礼物
    P2121 拆地毯
    Nebula Graph 在大规模数据量级下的实践和定制化开发
    深入了解kafka系列-消费者
    一分钟教你搭建WebRTC流媒体服务器Janus-gateway
    什么是"前端工程化"?
    斗鱼Juno 监控中心的设计与实现
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/10572178.html
Copyright © 2011-2022 走看看