Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add biweekly contest 130 #2793

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add biweekly contest 130
  • Loading branch information
yanglbme committed May 11, 2024
commit e21b5e5cf61caa2bcb51238bef2cf4da1839bfc7
109 changes: 72 additions & 37 deletions solution/0000-0099/0008.String to Integer (atoi)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,109 @@

<!-- 这里写题目描述 -->

<p>请你来实现一个&nbsp;<code>myAtoi(string s)</code>&nbsp;函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 <code>atoi</code> 函数)。</p>
<p>请你来实现一个&nbsp;<code>myAtoi(string s)</code>&nbsp;函数,使其能将字符串转换成一个 32 位有符号整数。</p>

<p>函数&nbsp;<code>myAtoi(string s)</code> 的算法如下:</p>

<ol>
<li>读入字符串并丢弃无用的前导空格</li>
<li>检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。</li>
<li>读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。</li>
<li>将前面步骤读入的这些数字转换为整数(即,"123" -&gt; 123, "0032" -&gt; 32)。如果没有读入数字,则整数为 <code>0</code> 。必要时更改符号(从步骤 2 开始)。</li>
<li>如果整数数超过 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>
<li>返回整数作为最终结果。</li>
<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><strong>注意:</strong></p>

<ul>
<li>本题中的空白字符只包括空格字符 <code>' '</code> 。</li>
<li>除前导空格或数字后的其余字符串外,<strong>请勿忽略</strong> 任何其他字符。</li>
</ul>
<p>返回整数作为最终结果。</p>

<p>&nbsp;</p>

<p><strong>示例&nbsp;1:</strong></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>
<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>示例&nbsp;2:</strong></p>
<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>
<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 class="example">示例&nbsp;3:</strong></p>

<p><strong>示例&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>
<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>&nbsp;</p>

Expand Down
56 changes: 33 additions & 23 deletions solution/0100-0199/0165.Compare Version Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,55 @@

<!-- 这里写题目描述 -->

<p>给你两个版本号 <code>version1</code> 和 <code>version2</code> ,请你比较它们。</p>
<p>给你两个 <strong>版本号字符串</strong>&nbsp;<code>version1</code> 和 <code>version2</code> ,请你比较它们。版本号由被点&nbsp;<code>'.'</code> 分开的修订号组成。<strong>修订号的值</strong> 是它 <strong>转换为整数</strong> 并忽略前导零。</p>

<p>版本号由一个或多个修订号组成,各修订号由一个 <code>'.'</code> 连接。每个修订号由 <strong>多位数字</strong> 组成,可能包含 <strong>前导零</strong> 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,<code>2.5.33</code> 和 <code>0.1</code> 都是有效的版本号。</p>

<p>比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 <strong>忽略任何前导零后的整数值</strong> 。也就是说,修订号 <code>1</code> 和修订号 <code>001</code> <strong>相等 </strong>。如果版本号没有指定某个下标处的修订号,则该修订号视为 <code>0</code> 。例如,版本 <code>1.0</code> 小于版本 <code>1.1</code> ,因为它们下标为 <code>0</code> 的修订号相同,而下标为 <code>1</code> 的修订号分别为 <code>0</code> 和 <code>1</code> ,<code>0 &lt; 1</code> 。</p>
<p>比较版本号时,请按 <strong>从左到右的顺序</strong> 依次比较它们的修订号。如果其中一个版本字符串的修订号较少,则将缺失的修订号视为 <code>0</code>。</p>

<p>返回规则如下:</p>

<ul>
<li>如果&nbsp;<code><em>version1&nbsp;</em>&gt;&nbsp;<em>version2</em></code>&nbsp;返回&nbsp;<code>1</code>,</li>
<li>如果&nbsp;<code><em>version1&nbsp;</em>&lt;&nbsp;<em>version2</em></code> 返回 <code>-1</code>,</li>
<li>如果&nbsp;<code><em>version1&nbsp;</em>&gt;&nbsp;<em>version2</em></code>&nbsp;返回&nbsp;<code>1</code>,</li>
<li>除此之外返回 <code>0</code>。</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.2", version2 = "1.10"</span></p>

<p><strong>输出:</strong><span class="example-io">-1</span></p>

<p><strong>解释:</strong></p>

<p>version1 的第二个修订号为&nbsp;"2",version2 的第二个修订号为 "10":2 &lt; 10,所以 version1 &lt; version2。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.01", version2 = "1.001"</span></p>

<p><strong>输出:</strong><span class="example-io">0</span></p>

<p><strong>解释:</strong></p>

<p>忽略前导零,"01" 和 "001" 都代表相同的整数 "1"。</p>
</div>

<pre>
<strong>输入:</strong>version1 = "1.01", version2 = "1.001"
<strong>输出:</strong>0
<strong>解释:</strong>忽略前导零,"01" 和 "001" 都表示相同的整数 "1"
</pre>
<p><strong class="example">示例 3:</strong></p>

<p><strong>示例 2:</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.0", version2 = "1.0.0.0"</span></p>

<pre>
<strong>输入:</strong>version1 = "1.0", version2 = "1.0.0"
<strong>输出:</strong>0
<strong>解释:</strong>version1 没有指定下标为 2 的修订号,即视为 "0"
</pre>
<p><strong>输出:</strong><span class="example-io">0</span></p>

<p><strong>示例 3:</strong></p>
<p><strong>解释:</strong></p>

<pre>
<strong>输入:</strong>version1 = "0.1", version2 = "1.1"
<strong>输出:</strong>-1
<strong>解释:</strong>version1 中下标为 0 的修订号是 "0",version2 中下标为 0 的修订号是 "1" 。0 &lt; 1,所以 version1 &lt; version2
</pre>
<p>version1 有更少的修订号,每个缺失的修订号按 "0" 处理。</p>
</div>

<p>&nbsp;</p>

Expand Down
114 changes: 37 additions & 77 deletions solution/0400-0499/0422.Valid Word Square/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,98 +8,58 @@

<!-- 这里写题目描述 -->

<p>给你一个单词序列,判断其是否形成了一个有效的单词方块。</p>
<p>给你一个字符串数组 <code>words</code>,如果它能形成一个有效的<strong> 单词方块 </strong>,则返回 <code>true</code> <em>。</em></p>

<p>有效的单词方块是指此由单词序列组成的文字方块的&nbsp;第 k 行 和&nbsp;第 k 列 (0 ≤ <em>k</em> &lt; max(行数, 列数)) 所显示的字符串完全相同。</p>

<p><strong>注意:</strong></p>

<ol>
<li>给定的单词数大于等于 1 且不超过 500。</li>
<li>单词长度大于等于 1 且不超过 500。</li>
<li>每个单词只包含小写英文字母&nbsp;<code>a-z</code>。</li>
</ol>
<p>有效的单词方块是指此由字符串数组组成的文字方块的&nbsp;第 <code>k</code> 行 和&nbsp;第 <code>k</code> 列所显示的字符串完全相同,其中 <code>0 &lt;= k &lt; max(numRows, numColumns)</code> 。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840723-rPoAKZ-image.png" style="width: 333px; height: 333px;" /></strong></p>

<p><strong class="example">示例 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/validsq1-grid.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>输入:</strong>
[
"abcd",
"bnrt",
"crmy",
"dtye"
]

<strong>输出:</strong>
true

<strong>解释:</strong>
第 1 行和第 1 列都是 "abcd"。
第 2 行和第 2 列都是 "bnrt"。
第 3 行和第 3 列都是 "crmy"。
第 4 行和第 4 列都是 "dtye"。

因此,这是一个有效的单词方块。
<strong>输入:</strong> words = ["abcd","bnrt","crmy","dtye"]
<strong>输出:</strong> true
<strong>解释:</strong>
第 1 行和第 1 列都读作 "abcd"。
第 2 行和第 2 列都读作 "bnrt"。
第 3 行和第 3 列都读作 "crmy"。
第 4 行和第 4 列都读作 "dtye"。
因此,它构成了一个有效的单词方块。
</pre>

<p>&nbsp;</p>

<p><strong>示例 2:</strong></p>

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840732-EvBDzU-image.png" style="width: 333px; height: 333px;" /></strong></p>

<p><strong class="example">示例 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/validsq2-grid.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>输入:</strong>
[
"abcd",
"bnrt",
"crm",
"dt"
]

<strong>输出:</strong>
true

<strong>解释:</strong>
第 1 行和第 1 列都是 "abcd"。
第 2 行和第 2 列都是 "bnrt"。
第 3 行和第 3 列都是 "crm"。
第 4 行和第 4 列都是 "dt"。

因此,这是一个有效的单词方块。
<strong>输入:</strong> words = ["abcd","bnrt","crm","dt"]
<strong>输出:</strong> true
<strong>解释:</strong>
第 1 行和第 1 列都读作 "abcd"。
第 2 行和第 2 列都读作 "bnrt"。
第 3 行和第 3 列都读作 "crm"。
第 4 行和第 4 列都读作 "dt"。
因此,它构成了一个有效的单词方块。
</pre>

<p>&nbsp;</p>

<p><strong>示例 3:</strong></p>

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/1713840738-tdOYTB-image.png" style="width: 333px; height: 333px;" /></strong></p>

<p><strong class="example">示例 3:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0422.Valid%20Word%20Square/images/validsq3-grid.jpg" style="width: 333px; height: 333px;" />
<pre>
<strong>输入:</strong>
[
"ball",
"area",
"read",
"lady"
]

<strong>输出:</strong>
false

<strong>解释:</strong>
第 3 行是 "read" ,然而第 3 列是 "lead"。

因此,这 <strong>不是</strong> 一个有效的单词方块。
<strong>输入:</strong> words = ["ball","area","read","lady"]
<strong>输出:</strong> false
<strong>解释:</strong>
第 3 行读作 "read" 而第 3 列读作 "lead"。
因此,它不构成一个有效的单词方块。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= words.length &lt;= 500</code></li>
<li><code>1 &lt;= words[i].length &lt;= 500</code></li>
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
</ul>

## 解法

### 方法一:遍历检查
Expand Down
Loading
Loading