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;
        }
     
  • 相关阅读:
    二分优化lis和STL函数
    D8 双连通分量
    Apicloud 之按两次后退键退出应用
    Js 之正则验证手机号、QQ、身份证等
    PHP 之循环创建文件夹
    招聘系统
    Mysql 之根据经纬度按距离排序
    PHP 之根据两个经纬度计算距离
    PHP 之Mysql优化
    Tkinter 之使用PAGE工具开发GUI界面
  • 原文地址:https://www.cnblogs.com/lingtingvfengsheng/p/4556647.html
Copyright © 2011-2022 走看看