# [8. String to Integer (atoi) (Medium)](https://leetcode.com/problems/string-to-integer-atoi/)
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer (similar to C/C++'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>"123" -> 123</code>, <code>"0032" -> 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> </p>
<p><strong>Example 1:</strong></p>
<pre><strong>Input:</strong> s = "42"
<strong>Output:</strong> 42
<strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
^
Step 3: "<u>42</u>" ("42" is read in)
^
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.
</pre>
<p><strong>Example 2:</strong></p>
<pre><strong>Input:</strong> s = " -42"
<strong>Output:</strong> -42
<strong>Explanation:</strong>
Step 1: "<u> </u>-42" (leading whitespace is read and ignored)
^
Step 2: " <u>-</u>42" ('-' is read, so the result should be negative)
^
Step 3: " -<u>42</u>" ("42" is read in)
^
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.
</pre>
<p><strong>Example 3:</strong></p>
<pre><strong>Input:</strong> s = "4193 with words"
<strong>Output:</strong> 4193
<strong>Explanation:</strong>
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
^
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
^
Step 3: "<u>4193</u> with words" ("4193" is read in; reading stops because the next character is a non-digit)
^
The parsed integer is 4193.
Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193.
</pre>
<p> </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>
**Companies**:
[Facebook](https://leetcode.com/company/facebook), [Microsoft](https://leetcode.com/company/microsoft), [Amazon](https://leetcode.com/company/amazon), [Bloomberg](https://leetcode.com/company/bloomberg), [Google](https://leetcode.com/company/google), [Goldman Sachs](https://leetcode.com/company/goldman-sachs), [Adobe](https://leetcode.com/company/adobe), [Qualtrics](https://leetcode.com/company/qualtrics)
**Related Topics**:
[String](https://leetcode.com/tag/string/)
**Similar Questions**:
* [Reverse Integer (Medium)](https://leetcode.com/problems/reverse-integer/)
* [Valid Number (Hard)](https://leetcode.com/problems/valid-number/)
* [Check if Numbers Are Ascending in a Sentence (Easy)](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/)
## Solution 1.
```cpp
// OJ: https://leetcode.com/problems/string-to-integer-atoi/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int myAtoi(string s) {
int i = 0, N = s.size(), sign = 1, ans = 0;
while (i < N && s[i] == ' ') ++i;
if (i < N && (s[i] == '+' || s[i] == '-')) sign = s[i++] == '+' ? 1 : -1;
while (i < N && isdigit(s[i])) {
int n = s[i++] - '0';
if (ans > INT_MAX / 10 || (ans == INT_MAX / 10 && n > INT_MAX % 10)) return sign == 1 ? INT_MAX : INT_MIN;
ans = ans * 10 + n;
}
return ans * sign;
}
};
```
c++-c++编程基础之leetcode题解第8题字符串转换整数.zip
需积分: 1 163 浏览量
2024-03-18
05:04:23
上传
评论
收藏 3KB ZIP 举报
DdddJMs__135
- 粉丝: 674
- 资源: 210