|
| 1 | +package com.bl.string_to_integer_atoi; |
| 2 | + |
| 3 | +/** |
| 4 | + * This is the solution to the LeetCode problem: 8. String to Integer (atoi) |
| 5 | + * |
| 6 | + * @author Børre A. Opedal Lunde |
| 7 | + * @since 2024.01.25 |
| 8 | + */ |
| 9 | +public class Solution { |
| 10 | + |
| 11 | + static final char WHITESPACE = ' '; |
| 12 | + |
| 13 | + static final char PLUS = '+'; |
| 14 | + static final char MINUS = '-'; |
| 15 | + |
| 16 | + static final int MINIMUM_VALUE = 0x80000000; // -2^31 |
| 17 | + static final int MAXIMUM_VALUE = 0x7fffffff; // 2^31 - 1 |
| 18 | + |
| 19 | + public int myAtoi(String s) { |
| 20 | + |
| 21 | + // If the string is empty, return zero. |
| 22 | + if (s.isEmpty()) { |
| 23 | + return 0; |
| 24 | + } |
| 25 | + |
| 26 | + // Convert the string to a character array that we'll iterate over. |
| 27 | + final char[] characters = s.toCharArray(); |
| 28 | + |
| 29 | + // Used to keep track of whether the number is negative or not. |
| 30 | + boolean negative = false; |
| 31 | + |
| 32 | + // This is the index used to iterate over the character array. It is |
| 33 | + // declared outside the loop so that it can be used in both loops. |
| 34 | + int i = 0; |
| 35 | + |
| 36 | + // First, ignore leading whitespace and account for explicit sign |
| 37 | + // symbol. |
| 38 | + for (; i < characters.length; i++) { |
| 39 | + char character = characters[i]; |
| 40 | + |
| 41 | + // Ignore leading whitespace. |
| 42 | + if (character == WHITESPACE) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + // Account for explicit sign symbol. |
| 47 | + else if (character == PLUS) { |
| 48 | + // The number is positive by default, so we don't need to do |
| 49 | + // set the negative flag to false. |
| 50 | + |
| 51 | + // Increment the index to skip the sign symbol. |
| 52 | + i++; |
| 53 | + } else if (character == MINUS) { |
| 54 | + negative = true; |
| 55 | + |
| 56 | + // Same as above. |
| 57 | + i++; |
| 58 | + } |
| 59 | + |
| 60 | + // If the character is not whitespace nor a sign symbol, we will |
| 61 | + // delegate the rest of the work to the next loop. It is responsible |
| 62 | + // for parsing the number. |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + // We're parsing an integer, but we're using a long to avoid overflow |
| 67 | + // and underflow. |
| 68 | + long number = 0; |
| 69 | + |
| 70 | + // Parse the number. |
| 71 | + for (; i < characters.length; i++) { |
| 72 | + final char character = characters[i]; |
| 73 | + |
| 74 | + // If the character is not a digit, we will break out of the loop. |
| 75 | + if (! characterIsDigit(character)) { |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + // Convert the character to a digit. |
| 80 | + final int digit = digitFromCharacter(character); |
| 81 | + |
| 82 | + // First, multiply the number by ten to make room for the new digit. |
| 83 | + // Then, add the digit to the number, taking into account whether |
| 84 | + // the number is negative or not. |
| 85 | + number = number * 10 + (negative ? - digit : digit); |
| 86 | + |
| 87 | + // Clamp the number if it is greater than the maximum value. |
| 88 | + if (number <= MINIMUM_VALUE) { |
| 89 | + return MINIMUM_VALUE; |
| 90 | + } |
| 91 | + |
| 92 | + // Clamp the number if it is less than the minimum value. |
| 93 | + else if (number >= MAXIMUM_VALUE) { |
| 94 | + return MAXIMUM_VALUE; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Return the number as an integer. |
| 99 | + return (int) number; |
| 100 | + } |
| 101 | + |
| 102 | + static int digitFromCharacter(final char character) { |
| 103 | + // Assumes the digit character is in the ASCII table. |
| 104 | + return character - '0'; |
| 105 | + } |
| 106 | + |
| 107 | + static boolean characterIsDigit(final char character) { |
| 108 | + // Assumes the character is in the ASCII table. |
| 109 | + return character >= '0' && character <= '9'; |
| 110 | + } |
| 111 | +} |
0 commit comments