• [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;
        }
     
  • 相关阅读:
    2018年产品设计协作领域最强黑马居然是它?
    中小企业网页设计,轻松拓展网络市场
    那些年,UI设计师还在手工标注和切图时走的弯路
    据说,攻城狮也可以像设计狮一样切图了?
    拼图类APP原型模板分享——简拼
    UI设计初学者必看,这款设计神器教你快速入门
    设计交付指南:设计师与开发如何才能好好协作?
    用户体验设计师面试中可能会问及的10个高频问题
    产品,设计和开发,高效协同只差一份文档
    素数
  • 原文地址:https://www.cnblogs.com/lingtingvfengsheng/p/4556647.html
Copyright © 2020-2023  润新知