zoukankan      html  css  js  c++  java
  • [leetcode 8] String to Integer

    1 题目:

    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.

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

    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.

    Hide Tags
     Math String
     
    2 思路
    按照字符串变成字符数组一位一位处理。
    考虑以下特殊情况:
    • 前面有空格
    • 整型越界
    • 有小数点
    • 判断正负
    • 非数字字符的处理,包括数字前面和后面
    3 代码:
        public int myAtoi(String str) {
            if(str.equals("")) return 0;
            //1 handle space
            str = str.trim();
            char[] chars = str.toCharArray();
            int len = str.length();
            long number = 0;
            boolean sign = true;
            
            // handle sign
            if(chars[0] >= '0' && chars[0] <= '9'){
                number += chars[0]  - 48;
            }else if(chars[0] == '-'){
                sign = false;
            }else if(chars[0] == '+'){
                sign = true;
            }else{
                return 0;   
            }
          
            // convern to number
            for(int i = 1; i < len; i++){
                if(chars[i] >= '0' && chars[i] <= '9'){
                    number = number*10 + (chars[i] - 48);
                    //handle overflow
                    if(number > Integer.MAX_VALUE){
                        return sign ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                    }
                }else{
                    number =  sign ? number : -number;
                    return (int)number;
                }
            }
            number =  sign ? number : -number;
          
            return (int)number;
        }
     
  • 相关阅读:
    C++中的内存分配
    进程间通信(IPC)
    一段重新开始的旅程
    状态压缩模版3:地雷
    状态压缩模版2:选数
    状态压缩模版1:滑块
    后缀数组练习4:Life Forms
    后缀数组练习3:连续重复子串
    后缀数组练习2:可重叠的k次最长重复子串
    后缀数组练习1:不可重叠最长重复子串
  • 原文地址:https://www.cnblogs.com/lingtingvfengsheng/p/4556647.html
Copyright © 2011-2022 走看看