GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/008_String_to_Integer(atoi).java at master · adi928/leetcode Expand file tree/ Copy path008_String_to_Integer(atoi).javaMore file actions 25 lines (25 loc) · 695 Bytes / Copy path008_String_to_Integer(atoi).javaFile metadata and controls25 lines (25 loc) · 695 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Solution { // example in leetcode book private static final int maxDiv10 = Integer.MAX_VALUE / 10; public int myAtoi(String str) { int i = 0, n = str.length(); while (i < n && Character.isWhitespace(str.charAt(i))) i++; int sign = 1; if (i < n && str.charAt(i) == '+') i++; else if (i < n && str.charAt(i) == '-') { sign = -1; i++; } int num = 0; while (i < n && Character.isDigit(str.charAt(i))) { int digit = Character.getNumericValue(str.charAt(i)); if (num > maxDiv10 || num == maxDiv10 && digit >= 8) return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; num = num * 10 + digit; i++; } return sign * num; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |