原题
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.题目大意
实现一个atoi函数,将字符串转成整形
要点:考虑所有的输入情况。解题思路
前导字符是+或-或者没有,接下来输入的是数字,数字不能整数能表示的最大或最小数。如果超过就返回对应的最小或者最小的值。
代码实现
public class Solution { public int atoi(String str) { if (str == null || str.length() == 0) {// throw new NumberFormatException("Invalid input string: " + str); return 0; } // 如果字符串以空格开始 int start = 0; //从开始找第一个不是空格的数 boolean positive = true; // 是否为正数默认为true if (str.charAt(start) == ' ') { while (str.charAt(start) == ' ') { start++; if (start >= str.length()) { // 输入的全是空格// throw new NumberFormatException("Invalid input string: " + str); return 0; } } } if (str.charAt(start) == '-') { // 第一个非空白字符中- positive = false; start++; } else if (str.charAt(start) == '+') {// 第一个非空白字符是+ start++; } else if (str.charAt(start) >= '0' && str.charAt(start) <= '9') { // 第一个非空白字符是数字 return cal(str, start, true); } else { // 其它情况就抛出异常// throw new NumberFormatException("Invalid input string: " + str); return 0; } if (start >= str.length()) { // 第一个非空白字符是+或者-但也是最后一个字符// throw new NumberFormatException("Invalid input string: " + str); return 0; } if (str.charAt(start) > '9' || str.charAt(start) < '0') { // +或者-后面接的不是数字// throw new NumberFormatException("Invalid input string: " + str); return 0; } else { return cal(str, start, positive); } } private int cal(String str, int start, boolean positive) { long result = 0; while (start < str.length() && str.charAt(start) >= '0' && str.charAt(start) <= '9') { result = result * 10 + (str.charAt(start) - '0'); if (positive) { // 如果是正数 if (result > Integer.MAX_VALUE) {// throw new NumberFormatException("Invalid input string: " + str); return Integer.MAX_VALUE; } } else { if (-result < Integer.MIN_VALUE) {// throw new NumberFormatException("Invalid input string: " + str); return Integer.MIN_VALUE; } } start++; } if (positive) { return (int) result; } else { return (int) -result; } }}
1、如何将字符串String转化为整数int int i = Integer.parseInt(str); int i = Integer.valueOf(my_str).intValue(); 注: 字串转成Double, Float, Long的方法大同小异。 2、如何将字符串String转化为Integer Integer integer=Integer.valueOf(i) 3、如何将整数 int 转换成字串 String? 答:有三种方法: String s = String.valueOf(i); String s = Integer.toString(i); String s = "" + i; 注:Double, Float, Long 转成字串的方法大同小异。 4、如何将整数int转化为Integer Integer integer=new Integer(i) 5、如何将Integer转化为字符串String Integer integer=String() 6、如何将Integer转化为int int num=Integer.intValue() 7、如何将String转化为BigDecimal BigDecimal d_id=new BigDecimal(str)
1、思路及注意事项
参考:
概括起来有几种情况
1)字符串开头是“+”号或“-”号的处理
2)非法字符的判断(不是数字)
3)整数溢出问题。
看看Java函数库中的Integer.parseInt(String sting)的源码如何处理这些问题的。
/** * Parses the specified string as a signed decimal integer value. The ASCII * character \u002d ('-') is recognized as the minus sign. * * @param string * the string representation of an integer value. * @return the primitive integer value represented by {@code string}. * @throws NumberFormatException * if {@code string} cannot be parsed as an integer value. */public static int parseInt(String string) throws NumberFormatException { return parseInt(string, 10);} /** * Parses the specified string as a signed integer value using the specified * radix. The ASCII character \u002d ('-') is recognized as the minus sign. * * @param string * the string representation of an integer value. * @param radix * the radix to use when parsing. * @return the primitive integer value represented by {@code string} using * {@code radix}. * @throws NumberFormatException * if {@code string} cannot be parsed as an integer value, * or {@code radix < Character.MIN_RADIX || * radix > Character.MAX_RADIX}. */public static int parseInt(String string, int radix) throws NumberFormatException { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (string == null) { throw invalidInt(string); } int length = string.length(), i = 0; if (length == 0) { throw invalidInt(string); } boolean negative = string.charAt(i) == '-'; if (negative && ++i == length) { throw invalidInt(string); } return parse(string, i, radix, negative);} private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException { int max = Integer.MIN_VALUE / radix; int result = 0, length = string.length(); while (offset < length) { int digit = Character.digit(string.charAt(offset++), radix); if (digit == -1) { throw invalidInt(string); } if (max > result) { throw invalidInt(string); } int next = result * radix - digit; if (next > result) { throw invalidInt(string); } result = next; } if (!negative) { result = -result; if (result < 0) { throw invalidInt(string); } } return result;}
parseInt(String string, int radix)判断了
1) radix进制超出范围 ( Character. MIN_RADIX = 2, Character. MAX_RADIX )=36)
2)字符串为null
3)字符串长度为空
4)字符串第一位为“-”且只有一位
没有异常之后进行 parse(String string, int offset, int radix, boolean negative) 判断,参数即字符串,偏移量,进制, negative (如果开头没有“-”则offset=0,negative=false,否则为offset=1,neagtive=true)
在 parse(String string, int offset, int radix, boolean negative)主要进行了溢出的判断。利用 offset++来控制移动, 在 while (offset < length) 循环中 直到倒数 第二位的时候,如果已经 小于 max = Integer.MIN_VALUE / radix 的话表明一定会溢出。例如"-2147483648"
倒数第二位的时候 :result= -214748364,max = -214748364,max>result不成立表明 可以进行最后一位的处理。
这里为什么不先求得当前的结果再同 Integer.MIN_VALUE比较?而是先同 Integer.MIN_VALUE / radix比较再决定是否进行下一位的添加? 不言而喻。
2、参考源码实
现字符串转化为整数
可以对比 。
public class StringToIntTest { /** * @author 曹艳丰 北京大学 */ public static void main(String[] args) { // TODO 自动生成的方法存根 try { System.out.println(parseInt("cao21'''474fefda8364fe7")); System.out.println(parseInt("-2147483648")); System.out.println(parseInt("-2147483651")); System.out.println(parseInt("-2147483648")); System.out.println(parseInt("-21474836410")); } catch (MyException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private static int parseInt(String string) throws MyException { /* 异常情况1:字符串为null */ if (string == null) { throw new MyException("字符串为null!"); } int length = string.length(), offset = 0; /* 异常情况2:字符串长度为0 */ if (length == 0) { throw new MyException("字符串长度为0!"); } boolean negative = string.charAt(offset) == '-'; /* 异常情况3:字符串为'-' */ if (negative && ++offset == length) { throw new MyException("字符串为:'-'!"); } int result = 0; char[] temp = string.toCharArray(); while (offset < length) { char digit = temp[offset++]; if (digit <= '9' && digit >= '0') { int currentDigit = digit - '0'; /* * 异常情况4:已经等于Integer.MAX_VALUE / 10,判断要添加的最后一位的情况: * 如果是负数的话,最后一位最大是8 如果是正数的话最后一位最大是7 */ if (result == Integer.MAX_VALUE / 10) { if ((negative == false && currentDigit > 7) || (negative && currentDigit > 8)) { throw new MyException("溢出!"); } /* * 异常情况5:已经大于Integer.MAX_VALUE / 10 * 无论最后一位是什么都会超过Integer.MAX_VALUE */ } else if (result > Integer.MAX_VALUE / 10) { throw new MyException("溢出!"); } int next = result * 10 + currentDigit; result = next; } } if (negative) { result = -result; } return result; } } /* 自定义异常 */class MyException extends Exception { /** * */ private static final long serialVersionUID = 1749149488419303367L; String message; public MyException(String message) { // TODO 自动生成的构造函数存根 this.message = message; } @Override public String getMessage() { // TODO 自动生成的方法存根 return message; } }
****************************
“如果是负数的话,最后一位最大是8 如果是正数的话最后一位最大是7”可以用Integer.MIN_VALUE%10和 Integer.MAX_VALUE%10来求。