mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-29 08:43:16 +08:00
批量更新数据
This commit is contained in:
@@ -1,71 +1,106 @@
|
||||
<p>请你来实现一个 <code>myAtoi(string s)</code> 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 <code>atoi</code> 函数)。</p>
|
||||
<p>请你来实现一个 <code>myAtoi(string s)</code> 函数,使其能将字符串转换成一个 32 位有符号整数。</p>
|
||||
|
||||
<p>函数 <code>myAtoi(string s)</code> 的算法如下:</p>
|
||||
|
||||
<ol>
|
||||
<li>读入字符串并丢弃无用的前导空格</li>
|
||||
<li>检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。</li>
|
||||
<li>读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。</li>
|
||||
<li>将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 <code>0</code> 。必要时更改符号(从步骤 2 开始)。</li>
|
||||
<li>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>, 2<sup>31 </sup>− 1]</code> ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被固定为 <code>−2<sup>31</sup></code> ,大于 <code>2<sup>31 </sup>− 1</code> 的整数应该被固定为 <code>2<sup>31 </sup>− 1</code> 。</li>
|
||||
<li>返回整数作为最终结果。</li>
|
||||
<li><strong>空格:</strong>读入字符串并丢弃无用的前导空格(<code>" "</code>)</li>
|
||||
<li><strong>符号:</strong>检查下一个字符(假设还未到字符末尾)为 <code>'-'</code> 还是 <code>'+'</code>。如果两者都不存在,则假定结果为正。</li>
|
||||
<li><strong>转换:</strong>通过跳过前置零来读取该整数,直到遇到非数字字符或到达字符串的结尾。如果没有读取数字,则结果为0。</li>
|
||||
<li><b>舍入:</b>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>, 2<sup>31 </sup>− 1]</code> ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被舍入为 <code>−2<sup>31</sup></code> ,大于 <code>2<sup>31 </sup>− 1</code> 的整数应该被舍入为 <code>2<sup>31 </sup>− 1</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>本题中的空白字符只包括空格字符 <code>' '</code> 。</li>
|
||||
<li>除前导空格或数字后的其余字符串外,<strong>请勿忽略</strong> 任何其他字符。</li>
|
||||
</ul>
|
||||
<p>返回整数作为最终结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<p><strong class="example">示例 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>
|
||||
<strong>输入:</strong>s = "42"
|
||||
<strong>输出:</strong>42
|
||||
<strong>解释:</strong>加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
|
||||
带下划线线的字符是所读的内容,插入符号是当前读入位置。
|
||||
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
|
||||
^
|
||||
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
|
||||
^
|
||||
第 3 步:"<u>42</u>"(读入 "42")
|
||||
^
|
||||
解析得到整数 42 。
|
||||
由于 "42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 42 。</pre>
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<p><strong class="example">示例 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>
|
||||
<strong>输入:</strong>s = " -42"
|
||||
<strong>输出:</strong>-42
|
||||
<strong>解释:</strong>
|
||||
第 1 步:"<u><strong> </strong></u>-42"(读入前导空格,但忽视掉)
|
||||
第 1 步:"<u><strong> </strong></u>-042"(读入前导空格,但忽视掉)
|
||||
^
|
||||
第 2 步:" <u><strong>-</strong></u>42"(读入 '-' 字符,所以结果应该是负数)
|
||||
第 2 步:" <u>-</u>042"(读入 '-' 字符,所以结果应该是负数)
|
||||
^
|
||||
第 3 步:" <u><strong>-42</strong></u>"(读入 "42")
|
||||
第 3 步:" <u>-042</u>"(读入 "042",在结果中忽略前导零)
|
||||
^
|
||||
解析得到整数 -42 。
|
||||
由于 "-42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 -42 。
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<p><strong class="example">示例 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>
|
||||
<strong>输入:</strong>s = "4193 with words"
|
||||
<strong>输出:</strong>4193
|
||||
<strong>解释:</strong>
|
||||
第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
|
||||
第 1 步:"1337c0d3"(当前没有读入字符,因为没有前导空格)
|
||||
^
|
||||
第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
|
||||
第 2 步:"1337c0d3"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
|
||||
^
|
||||
第 3 步:"<u>4193</u> with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
|
||||
第 3 步:"1337c0d3"(读入 "1337";由于下一个字符不是一个数字,所以读入停止)
|
||||
^
|
||||
解析得到整数 4193 。
|
||||
由于 "4193" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 4193 。
|
||||
</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> </p>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user