• leetcode — string-to-integer-atoi


    /**
     * Source : https://oj.leetcode.com/problems/string-to-integer-atoi/
     *
     * Created by lverpeng on 2017/7/4.
     *
     * 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.
     *
     *
     * 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.
     *
     */
    public class IntegerParser {
    
        /**
         * 将字符串转化为int
         * 考虑各种输入:
         * 输入包含非数字
         * 超出int范围
         * 可能是负数
         *
         * @param str
         * @return
         */
        public int parse (String str) throws Exception {
            if (str == null || str.length() == 0) {
                return 0;
            }
            int result = 0;
            boolean flag = true;
            if (str.charAt(0) == '-') {
                flag = false;
                str = str.substring(1);
            } else if (str.charAt(0) == '+') {
                flag = true;
                str = str.substring(1);
            }
            for (int i = 0; i < str.length(); i++) {
                if (!isdDigit(str.charAt(i))) {
                    throw new IllegalArgumentException("is not a number");
                }
                if (result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10) {
                    throw new Exception("overflow");
                }
                result = result * 10 + str.charAt(i) - 48;
            }
            if (!flag) {
                result = 0 - result;
            }
            return result;
        }
    
        private boolean isdDigit (char c) {
            if (c >= 48 && c <= 57) {
                return true;
            }
            return false;
        }
    
        public static void main(String[] args) throws Exception {
            IntegerParser parser = new IntegerParser();
            System.out.println(parser.parse("123"));
            System.out.println(parser.parse("+123"));
            System.out.println(parser.parse("-123"));
            System.out.println(parser.parse("123ABC"));
        }
    }
    
    
  • 相关阅读:
    精通javascript、javascript语言精粹读书笔记
    javascript语言精粹。3、对象;4、函数;深入原型链,对象的原理!
    权限管理
    javascript碎碎念(面向对象备忘)
    几个JavaScript面试题
    JavaScript类属性的定义方法和区别
    j
    Java调用SQL存储过程 输入输出参数(转)
    Java中文乱码解决
    js中eval详解
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7266191.html
Copyright © 2020-2023  润新知