• 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 :

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

  • 相关阅读:
    当梦想渐行渐远的时候
    android开发系列之aidl
    看到这页代码,我要疯了
    android开发系列之性能优化
    近期的一个项目反思与总结
    IOS中摇一摇实现截屏(可实现问题反馈的功能)
    IOS设计模式浅析之外观模式(Facade)
    IOS项目开发中的文件和文件夹操作
    XCode 4.6下XIB文件提示“...could not be opened..."的问题
    "Xcode"意外退出
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/10572178.html
Copyright © 2020-2023  润新知