Class Solution

java.lang.Object
g0001_0100.s0008_string_to_integer_atoi.Solution

public class Solution extends Object
8 - String to Integer (atoi).<p>Medium</p> <p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer (similar to C/C++&rsquo;s <code>atoi</code> function).</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li>Read in and ignore any leading whitespace.</li> <li>Check if the next character (if not already at the end of the string) is <code>'-'</code> or <code>'+'</code>. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.</li> <li>Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.</li> <li>Convert these digits into an integer (i.e. <code>&quot;123&quot; -> 123</code>, <code>&quot;0032&quot; -> 32</code>). If no digits were read, then the integer is <code>0</code>. Change the sign as necessary (from step 2).</li> <li>If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>.</li> <li>Return the integer as the final result.</li> </ol> <p><strong>Note:</strong></p> <ul> <li>Only the space character <code>' '</code> is considered a whitespace character.</li> <li><strong>Do not ignore</strong> any characters other than the leading whitespace or the rest of the string after the digits.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;42&rdquo;</p> <p><strong>Output:</strong> 42</p> <p><strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position.</p> <pre><code> Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a '-' nor '+') ^ Step 3: &quot;42&quot; (&quot;42&quot; is read in) ^ </code></pre> <p>The parsed integer is 42. Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &quot; -42&quot;</p> <p><strong>Output:</strong> -42</p> <p><strong>Explanation:</strong></p> <pre><code> Step 1: &quot; -42&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; -42&quot; ('-' is read, so the result should be negative) ^ Step 3: &quot; -42&quot; (&quot;42&quot; is read in) ^ The parsed integer is -42. </code></pre> <p>Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;4193 with words&rdquo;</p> <p><strong>Output:</strong> 4193</p> <p><strong>Explanation:</strong></p> <pre><code> Step 1: &quot;4193 with words&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;4193 with words&quot; (no characters read because there is neither a '-' nor '+') ^ Step 3: &quot;4193 with words&quot; (&quot;4193&quot; is read in; reading stops because the next character is a non-digit) ^ The parsed integer is 4193. </code></pre> <p>Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> s = &ldquo;words and 987&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <pre><code> Step 1: &quot;words and 987&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;words and 987&quot; (no characters read because there is neither a '-' nor '+') ^ Step 3: &quot;words and 987&quot; (reading stops immediately because there is a non-digit 'w') ^ The parsed integer is 0 because no digits were read. </code></pre> <p>Since 0 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 0.</p> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> s = &ldquo;-91283472332&rdquo;</p> <p><strong>Output:</strong> -2147483648</p> <p><strong>Explanation:</strong></p> <pre><code> Step 1: &quot;-91283472332&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;-91283472332&quot; ('-' is read, so the result should be negative) ^ Step 3: &quot;-91283472332&quot; (&quot;91283472332&quot; is read in) ^ The parsed integer is -91283472332. </code></pre> <p>Since -91283472332 is less than the lower bound of the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is clamped to -2<sup>31</sup> = -2147483648.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= s.length <= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>' '</code>, <code>'+'</code>, <code>'-'</code>, and <code>'.'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • myAtoi

      public int myAtoi(String str)