标签:code turn public bsp amp git nal har string
1 private static final int maxDiv10 = Integer.MAX_VALUE / 10; 2 3 public int atoi(String str) { 4 int i = 0; 5 int n = str.length(); 6 // 首先忽略字符串前面的空格 7 while(i < n && Character.isWhitespace(str.charAt(i))) { 8 i++; 9 } 10 11 int sign = 1; 12 if(i < n && str.charAt(i) == ‘+‘) { 13 i++; 14 } else if(i < n && str.charAt(i) == ‘-‘) { 15 sign = -1; 16 i++; 17 } 18 19 int num = 0; 20 while(i < n && Character.isDigit(str.charAt(i))) { 21 int digit = Character.getNumericValue(str.charAt(i)); 22 if(num > maxDiv10 || num == maxDiv10 && digit >= 8) { 23 return sign == 1?Integer.MAX_VALUE:Integer.MIN_VALUE; 24 } 25 26 num = num * 10 + digit; 27 i++; 28 } 29 30 return sign * num; 31 }
标签:code turn public bsp amp git nal har string
原文地址:https://www.cnblogs.com/wylwyl/p/10348921.html