美文网首页
67:把字符串转换成整数

67:把字符串转换成整数

作者: stoneyang94 | 来源:发表于2018-08-28 14:04 被阅读0次

题目67:把字符串转换成整数

实现一个函数stringToInt,实现把字符串转换成整数这个功能,不能使用atoi或者其他类似的库函数。

分析

该题目主要考察的就是边界条件:

  • 空字符串
    if (num == null || num.length() < 1)
  • 在数字范围
    c >= '0' && c <= '9'
  • 数据上下溢出
    if (tmp > 0x8000_0000L)
  • 只有正负号
  • 有无正负号
    if (first == '-')
    else if (first == '+')
  • 错误标志输出
    throw new NumberFormatException(num)

思路

代码实现

public class _67 {
    public static int stringToInt(String num) {
        if (num == null || num.length() < 1) {
            throw new NumberFormatException(num);
        }
        char first = num.charAt(0);
        if (first == '-') {
            return parseString(num, 1, false);
        } else if (first == '+') {
            return parseString(num, 1, true);
        } else if (first <= '9' && first >= '0') {
            return parseString(num, 0, true);
        } else {
            throw new NumberFormatException(num);
        }
    }

    private static boolean isDigit(char c) {
        return c >= '0' && c <= '9';
    }

    private static int parseString(String num, int index, boolean positive) {
        if (index >= num.length()) {//index    开始解析的索引位置
            throw new NumberFormatException(num);
        }

        int result;
        long tmp = 0;
        while (index < num.length() && isDigit(num.charAt(index))) {
            tmp = tmp * 10 + num.charAt(index) - '0';//转换的关键步骤
            // 保证求的得的值不超出整数的最大绝对值
            if (tmp > 0x8000_0000L) {
                throw new NumberFormatException(num);
            }
            index++;
        }
        if (positive) {//正数
            if (tmp >= 0x8000_0000L) {
                throw new NumberFormatException(num);
            } else {
                result = (int) tmp;
            }
        } else {//负号开头
            if (tmp == 0x8000_0000L) {
                result = 0x8000_0000;
            } else {
                result = (int) -tmp;
            }
        }//最后的长度判定,避免 1a123
        if(sizeOfInt(result)+index<num.length()) {
            throw new NumberFormatException(num);
        }
        return result;
    }
    
    //判断一个int值是几位数
    final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
            99999999, 999999999, Integer.MAX_VALUE };
    static int sizeOfInt(int x) {
        for (int i = 0; ; i++) {//从一位数开始对比,如果比一位数大便继续对比二位数
            if (x <= sizeTable[i]) {
                return i + 1;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("Integer.MIN_VALUE is"+Integer.parseInt(Integer.MIN_VALUE + ""));// -2147483648
        System.out.println("Integer.MAX_VALUE is "+Integer.parseInt(Integer.MAX_VALUE + ""));//2147483647
        System.out.println("123 -> "+stringToInt("123"));// 123
        System.out.println("+123 -> "+stringToInt("+123"));// 123
        System.out.println("-123 -> "+stringToInt("-123"));// -123
        System.out.println("+2147483647 -> "+stringToInt("+2147483647"));// 2147483647
        System.out.println("-2147483647 -> "+stringToInt("-2147483647"));// -2147483647
        System.out.println("-2147483648 -> "+stringToInt("-2147483648"));// -2147483648
//       System.out.println("1a123 -> "+stringToInt("1a123"));//java.lang.NumberFormatException: 1a123
//       System.out.println("+2147483649 -> "+stringToInt("+2147483649"));//java.lang.NumberFormatException:+2147483649
//       System.out.println("-2147483649 -> "+stringToInt("-2147483649"));//java.lang.NumberFormatException:-2147483649
//       System.out.println("空字符-> "+stringToInt(""));//java.lang.NumberFormatException
//       System.out.println("+2147483648 -> "+stringToInt("+2147483648"));//java.lang.NumberFormatException:+2147483648
//       System.out.println("+ ->"+stringToInt("+"));//java.lang.NumberFormatException: +
//       System.out.println("- ->"+stringToInt("-"));//java.lang.NumberFormatException: -
    }
}

输出:(抛异常的输出见注释)

Integer.MIN_VALUE is-2147483648
Integer.MAX_VALUE is 2147483647
123 -> 123
+123 -> 123
-123 -> -123
+2147483647 -> 2147483647
-2147483647 -> -2147483647
-2147483648 -> -2147483648

一个机灵:

 (res << 1) + (res << 3) 即 res*2+res*8=res*10 

相关文章

  • 67:把字符串转换成整数

    题目67:把字符串转换成整数 实现一个函数stringToInt,实现把字符串转换成整数这个功能,不能使用atoi...

  • 剑指offer第二版-67.把字符串转换成整数

    本系列导航:剑指offer(第二版)java实现导航帖 面试题67:把字符串转换成整数 题目要求:如题。 解题思路...

  • 把字符串转换成整数-java

    把字符串转换成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串...

  • JZ-049-把字符串转换成整数

    把字符串转换成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串...

  • 字符串转换成整数

    字符串转换成整数 题目描述: 输入一个由数字组成的字符串,把它转换成整数并输出。例如:输入字符串"123",输出整...

  • 剑指offer(四十九)把字符串转换成整数

    点击进入 牛客网题库:把字符串转换成整数 题目描述将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。...

  • 剑指 offer:49、把字符串转化成整数

    49. 把字符串转换成整数 题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string...

  • 数据转换

    1.int(x[,base])(1)把符合数学格式的数字型字符串转换成整数;(2)把浮点数转换成整数,但是只是简单...

  • python 数据类型转换

    强制类型转换 int 函数 把符合数学格式的数字型字符串转换成整数 把浮点数转换成整数,但是只是简单的取整,而非四...

  • 面试题67:把字符串转换成整数

    题目 写一个函数StrToInt,实现把字符串转换成整数的功能。要求不能使用atoi或其他类似的库函数。 解题思路...

网友评论

      本文标题:67:把字符串转换成整数

      本文链接:https://www.haomeiwen.com/subject/pddtwftx.html