Nav: << previous: 7.整数反转 | next: 9.回文数 >>


Description

tab: English
 
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p>
 
<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p>
 
<ol>
	<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
	<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity if neither present.</li>
	<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li>
	<li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
</ol>
 
<p>Return the integer as the final result.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">42</span></p>
 
<p><strong>Explanation:</strong></p>
 
<pre>
The underlined characters are what is read in and the caret is the current reader position.
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 &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in)
           ^
</pre>
</div>
 
<p><strong class="example">Example 2:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">-42</span></p>
 
<p><strong>Explanation:</strong></p>
 
<pre>
Step 1: &quot;<u>   </u>-042&quot; (leading whitespace is read and ignored)
            ^
Step 2: &quot;   <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative)
             ^
Step 3: &quot;   -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result)
               ^
</pre>
</div>
 
<p><strong class="example">Example 3:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">1337</span></p>
 
<p><strong>Explanation:</strong></p>
 
<pre>
Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace)
         ^
Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit)
             ^
</pre>
</div>
 
<p><strong class="example">Example 4:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 
<p><strong>Explanation:</strong></p>
 
<pre>
Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace)
         ^
Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)
         ^
Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit)
          ^
</pre>
</div>
 
<p><strong class="example">Example 5:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>Reading stops at the first non-digit character &#39;w&#39;.</p>
</div>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>0 &lt;= s.length &lt;= 200</code></li>
	<li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li>
</ul>
 
 
 
---
 
[submissions](https://leetcode.com/problems/string-to-integer-atoi/submissions/) | [solutions](https://leetcode.com/problems/string-to-integer-atoi/solutions/)
 
 
tab: 中文
 
<p>请你来实现一个&nbsp;<code>myAtoi(string s)</code>&nbsp;函数,使其能将字符串转换成一个 32 位有符号整数。</p>
 
<p>函数&nbsp;<code>myAtoi(string s)</code> 的算法如下:</p>
 
<ol>
	<li><strong>空格:</strong>读入字符串并丢弃无用的前导空格(<code>" "</code>)</li>
	<li><strong>符号:</strong>检查下一个字符(假设还未到字符末尾)为&nbsp;<code>'-'</code> 还是 <code>'+'</code>。如果两者都不存在,则假定结果为正。</li>
	<li><strong>转换:</strong>通过跳过前置零来读取该整数,直到遇到非数字字符或到达字符串的结尾。如果没有读取数字,则结果为0。</li>
	<li><b>舍入:</b>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>− 1]</code> ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被舍入为 <code>−2<sup>31</sup></code> ,大于 <code>2<sup>31&nbsp;</sup>− 1</code> 的整数应该被舍入为 <code>2<sup>31&nbsp;</sup>− 1</code> 。</li>
</ol>
 
<p>返回整数作为最终结果。</p>
 
<p>&nbsp;</p>
 
<p><strong class="example">示例&nbsp;1:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "42"</span></p>
 
<p><strong>输出:</strong><span class="example-io">42</span></p>
 
<p><strong>解释:</strong>加粗的字符串为已经读入的字符,插入符号是当前读取的字符。</p>
 
<pre>
带下划线线的字符是所读的内容,插入符号是当前读入位置。
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
         ^
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
         ^
第 3 步:"<u>42</u>"(读入 "42")
           ^
</pre>
</div>
 
<p><strong class="example">示例&nbsp;2:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = " -042"</span></p>
 
<p><strong>输出:</strong><span class="example-io">-42</span></p>
 
<p><strong>解释:</strong></p>
 
<pre>
第 1 步:"<u><strong>   </strong></u>-042"(读入前导空格,但忽视掉)
            ^
第 2 步:"   <u>-</u>042"(读入 '-' 字符,所以结果应该是负数)
             ^
第 3 步:"   <u>-042</u>"(读入 "042",在结果中忽略前导零)
               ^
</pre>
</div>
 
<p><strong class="example">示例&nbsp;3:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "</span>1337c0d3<span class="example-io">"</span></p>
 
<p><strong>输出:</strong><span class="example-io">1337</span></p>
 
<p><strong>解释:</strong></p>
 
<pre>
第 1 步:"1337c0d3"(当前没有读入字符,因为没有前导空格)
         ^
第 2 步:"1337c0d3"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
         ^
第 3 步:"1337c0d3"(读入 "1337";由于下一个字符不是一个数字,所以读入停止)
             ^
</pre>
</div>
 
<p><strong class="example">示例 4:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "0-1"</span></p>
 
<p><span class="example-io"><b>输出:</b>0</span></p>
 
<p><strong>解释:</strong></p>
 
<pre>
第 1 步:"0-1" (当前没有读入字符,因为没有前导空格)
         ^
第 2 步:"0-1" (当前没有读入字符,因为这里不存在 '-' 或者 '+')
         ^
第 3 步:"<u>0</u>-1" (读入 "0";由于下一个字符不是一个数字,所以读入停止)
          ^
</pre>
</div>
 
<p><strong class="example">示例 5:</strong></p>
 
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "words and 987"</span></p>
 
<p><strong>输出:</strong><span class="example-io">0</span></p>
 
<p><strong>解释:</strong></p>
 
<p>读取在第一个非数字字符“w”处停止。</p>
</div>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>0 &lt;= s.length &lt;= 200</code></li>
	<li><code>s</code> 由英文字母(大写和小写)、数字(<code>0-9</code>)、<code>' '</code>、<code>'+'</code>、<code>'-'</code> 和 <code>'.'</code> 组成</li>
</ul>
 
 
 
---
 
[提交记录](https://leetcode.cn/problems/string-to-integer-atoi/submissions/) | [题解](https://leetcode.cn/problems/string-to-integer-atoi/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122