标签:
Form today on, I changed to Math module question.
public int myAtoi(String str) { if (str == null || str.length() == 0) return 0; str = str.trim(); char flag = ‘+‘; int i = 0; if (str.charAt(0) == ‘-‘) { flag = ‘-‘; i++; } else if (str.charAt(0) == ‘+‘) { i++; } int result = 0; while (str.length() > i && str.charAt(i) >= ‘0‘ && str.charAt(i) <= ‘9‘) { result = result * 10 + (str.charAt(i) - ‘0‘); i++; } if (flag == ‘-‘) result = 0-result; // handle max and min if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE; return (int) result; }
标签:
原文地址:http://www.cnblogs.com/timoBlog/p/4649836.html