Skip to content

Commit 1da59b9

Browse files
committed
LeetCode 付费题目描述 前200题
1 parent 4923650 commit 1da59b9

File tree

16 files changed

+592
-16
lines changed

16 files changed

+592
-16
lines changed

solution/0100-0199/0156.Binary Tree Upside Down/README.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,46 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>给定一个二叉树,其中所有的右节点要么是具有兄弟节点(拥有相同父节点的左节点)的叶节点,要么为空,将此二叉树上下翻转并将它变成一棵树, 原来的右节点将转换成左叶节点。返回新的根。</p>
8+
9+
<p><strong>例子:</strong></p>
10+
11+
<pre><strong>输入: </strong>[1,2,3,4,5]
12+
13+
1
14+
/ \
15+
2 3
16+
/ \
17+
4 5
18+
19+
<strong>输出:</strong> 返回二叉树的根 [4,5,2,#,#,3,1]
20+
21+
4
22+
/ \
23+
5 2
24+
/ \
25+
3 1
26+
</pre>
27+
28+
<p><strong>说明:</strong></p>
29+
30+
<p>对 <code>[4,5,2,#,#,3,1]</code> 感到困惑? 下面详细介绍请查看&nbsp;<a href="https://support.leetcode-cn.com/hc/kb/article/1194353/" target="_blank">二叉树是如何被序列化的</a>。</p>
31+
32+
<p>二叉树的序列化遵循层次遍历规则,当没有节点存在时,&#39;#&#39; 表示路径终止符。</p>
33+
34+
<p>这里有一个例子:</p>
35+
36+
<pre> 1
37+
/ \
38+
2 3
39+
/
40+
4
41+
\
42+
5
43+
</pre>
44+
45+
<p>上面的二叉树则被序列化为 <code>[1,2,3,#,#,4,#,#,5]</code>.</p>
46+
847

948

1049
## 解法

solution/0100-0199/0156.Binary Tree Upside Down/README_EN.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,48 @@
33
[中文文档](/solution/0100-0199/0156.Binary%20Tree%20Upside%20Down/README.md)
44

55
## Description
6-
None
6+
<p>Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.</p>
7+
8+
<p><strong>Example:</strong></p>
9+
10+
<pre>
11+
<strong>Input: </strong>[1,2,3,4,5]
12+
13+
1
14+
/ \
15+
2 3
16+
/ \
17+
4 5
18+
19+
<strong>Output:</strong> return the root of the binary tree [4,5,2,#,#,3,1]
20+
21+
4
22+
/ \
23+
5 2
24+
/ \
25+
3 1
26+
</pre>
27+
28+
<p><strong>Clarification:</strong></p>
29+
30+
<p>Confused what <code>[4,5,2,#,#,3,1<font face="monospace">]</font></code>&nbsp;means? Read more below on how binary tree is serialized on OJ.</p>
31+
32+
<p>The serialization of a binary tree follows a level order traversal, where &#39;#&#39; signifies a path terminator where no node exists below.</p>
33+
34+
<p>Here&#39;s an example:</p>
35+
36+
<pre>
37+
1
38+
/ \
39+
2 3
40+
/
41+
4
42+
\
43+
5
44+
</pre>
45+
46+
<p>The above binary tree is serialized as <code>[1,2,3,#,#,4,#,#,5]</code>.</p>
47+
748

849

950
## Solutions

solution/0100-0199/0157.Read N Characters Given Read4/README.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,87 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>给你一个文件,并且该文件只能通过给定的&nbsp;<code>read4</code>&nbsp;方法来读取,请实现一个方法使其能够读取 n 个字符。</p>
8+
9+
<p><strong>read4 方法:</strong></p>
10+
11+
<p>API&nbsp;<code>read4</code>&nbsp;可以从文件中读取 4 个连续的字符,并且将它们写入缓存数组&nbsp;<code>buf</code>&nbsp;中。</p>
12+
13+
<p>返回值为实际读取的字符个数。</p>
14+
15+
<p>注意&nbsp;<code>read4()</code> 自身拥有文件指针,很类似于 C 语言中的 <code>FILE *fp</code> 。</p>
16+
17+
<p><strong>read4 的定义:</strong></p>
18+
19+
<pre>参数类型: char[] buf4
20+
返回类型: int
21+
22+
注意: buf4[] 是目标缓存区不是源缓存区,read4 的返回结果将会复制到 buf4[] 当中。
23+
</pre>
24+
25+
<p>下列是一些使用 <code>read4</code> 的例子:</p>
26+
27+
<p><img style="width: 600px;"></p>
28+
29+
<pre><code>File file(&quot;abcde&quot;); // 文件名为 &quot;abcde&quot;, 初始文件指针 (fp) 指向 &#39;a&#39;
30+
char[] buf4 = new char[4]; // 创建一个缓存区使其能容纳足够的字符
31+
read4(buf4); // read4 返回 4。现在 buf4 = &quot;abcd&quot;,fp 指向 &#39;e&#39;
32+
read4(buf4); // read4 返回 1。现在 buf4 = &quot;e&quot;,fp 指向文件末尾
33+
read4(buf4); // read4 返回 0。现在 buf = &quot;&quot;,fp 指向文件末尾</code></pre>
34+
35+
<p><strong>read 方法:</strong></p>
36+
37+
<p>通过使用 <code>read4</code> 方法,实现&nbsp;<code>read</code> 方法。该方法可以从文件中读取 n 个字符并将其存储到缓存数组&nbsp;<code>buf</code> 中。您&nbsp;<strong>不能&nbsp;</strong>直接操作文件。</p>
38+
39+
<p>返回值为实际读取的字符。</p>
40+
41+
<p><strong>read&nbsp;的定义:</strong></p>
42+
43+
<pre>参数类型: char[] buf, int n
44+
返回类型: int
45+
46+
注意: buf[] 是目标缓存区不是源缓存区,你需要将结果写入 buf[] 中。
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>示例 1:</strong></p>
52+
53+
<pre><strong>输入: </strong>file = &quot;abc&quot;, n = 4
54+
<strong>输出: </strong>3
55+
<strong>解释:</strong> 当执行你的 read 方法后,buf 需要包含 &quot;abc&quot;。 文件一共 3 个字符,因此返回 3。 注意 &quot;abc&quot; 是文件的内容,不是 buf 的内容,buf 是你需要写入结果的目标缓存区。 </pre>
56+
57+
<p><strong>示例 2:</strong></p>
58+
59+
<pre><strong>输入: </strong>file = &quot;abcde&quot;, n = 5
60+
<strong>输出: </strong>5
61+
<strong>解释: </strong>当执行你的 read 方法后,buf 需要包含 &quot;abcde&quot;。文件共 5 个字符,因此返回 5。
62+
</pre>
63+
64+
<p><strong>示例 3:</strong></p>
65+
66+
<pre><strong>输入: </strong>file = &quot;abcdABCD1234&quot;, n = 12
67+
<strong>输出: </strong>12
68+
<strong>解释: </strong>当执行你的 read 方法后,buf 需要包含 &quot;abcdABCD1234&quot;。文件一共 12 个字符,因此返回 12。
69+
</pre>
70+
71+
<p><strong>示例 4:</strong></p>
72+
73+
<pre><strong>输入: </strong>file = &quot;leetcode&quot;, n = 5
74+
<strong>输出: </strong>5
75+
<strong>解释:</strong> 当执行你的 read 方法后,buf 需要包含 &quot;leetc&quot;。文件中一共 5 个字符,因此返回 5。
76+
</pre>
77+
78+
<p>&nbsp;</p>
79+
80+
<p><strong>提示:</strong></p>
81+
82+
<ul>
83+
<li>你 <strong>不能</strong> 直接操作该文件,文件只能通过 <code>read4</code> 获取而 <strong>不能</strong> 通过 <code>read</code>。</li>
84+
<li><code>read</code>&nbsp; 函数只在每个测试用例调用一次。</li>
85+
<li>你可以假定目标缓存数组&nbsp;<code>buf</code> 保证有足够的空间存下 n 个字符。&nbsp;</li>
86+
</ul>
87+
888

989

1090
## 解法

solution/0100-0199/0157.Read N Characters Given Read4/README_EN.md

+94-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,100 @@
33
[中文文档](/solution/0100-0199/0157.Read%20N%20Characters%20Given%20Read4/README.md)
44

55
## Description
6-
None
6+
<p>Given a file and assume that you can only read the file using a given method&nbsp;<code>read4</code>, implement a method to read <em>n</em> characters.</p>
7+
8+
<p>&nbsp;</p>
9+
10+
<p><b>Method read4: </b></p>
11+
12+
<p>The API&nbsp;<code>read4</code> reads 4 consecutive characters from the file, then writes those characters into the buffer array <code>buf</code>.</p>
13+
14+
<p>The return value is the number of actual characters read.</p>
15+
16+
<p>Note that&nbsp;<code>read4()</code> has its own file pointer, much like <code>FILE *fp</code> in C.</p>
17+
18+
<p><b>Definition of read4:</b></p>
19+
20+
<pre>
21+
Parameter: char[] buf4
22+
Returns: int
23+
24+
Note: buf4[] is destination not source, the results from read4 will be copied to buf4[]
25+
</pre>
26+
27+
<p>Below is a high level example of how <code>read4</code> works:</p>
28+
29+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/01/157_example.png" style="width: 600px; height: 403px;" /></p>
30+
31+
<pre>
32+
<code>File file(&quot;</code>abcde<code>&quot;); // File is &quot;</code>abcde<code>&quot;, initially file pointer (fp) points to &#39;a&#39;
33+
char[] buf4 = new char[4]; // Create buffer with enough space to store characters
34+
read4(buf4); // read4 returns 4. Now buf = &quot;abcd&quot;, fp points to &#39;e&#39;
35+
read4(buf4); // read4 returns 1. Now buf = &quot;e&quot;, fp points to end of file
36+
read4(buf4); // read4 returns 0. Now buf = &quot;&quot;, fp points to end of file</code>
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>Method read:</strong></p>
42+
43+
<p>By using the <code>read4</code> method, implement the method&nbsp;<code>read</code> that reads <i>n</i> characters from the file and store it in the&nbsp;buffer array&nbsp;<code>buf</code>. Consider that you <strong>cannot</strong> manipulate the file directly.</p>
44+
45+
<p>The return value is the number of actual characters read.</p>
46+
47+
<p><b>Definition of read: </b></p>
48+
49+
<pre>
50+
Parameters: char[] buf, int n
51+
Returns: int
52+
53+
Note: buf[] is destination not source, you will need to write the results to buf[]
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
58+
<p><strong>Example 1:</strong></p>
59+
60+
<pre>
61+
<strong>Input: </strong>file = &quot;abc&quot;, n = 4
62+
<strong>Output: </strong>3
63+
<strong>Explanation:</strong>&nbsp;After calling your read method, buf should contain &quot;abc&quot;. We read a total of 3 characters from the file, so return 3. Note that &quot;abc&quot; is the file&#39;s content, not buf. buf is the destination buffer that you will have to write the results to.
64+
</pre>
65+
66+
<p><strong>Example 2:</strong></p>
67+
68+
<pre>
69+
<strong>Input: </strong>file = &quot;abcde&quot;, n = 5
70+
<strong>Output: </strong>5
71+
<strong>Explanation: </strong>After calling your read method, buf should contain &quot;abcde&quot;. We read a total of 5 characters from the file, so return 5.
72+
</pre>
73+
74+
<p><strong>Example 3:</strong></p>
75+
76+
<pre>
77+
<strong>Input: </strong>file = &quot;abcdABCD1234&quot;, n = 12
78+
<strong>Output: </strong>12
79+
<strong>Explanation: </strong>After calling your read method, buf should contain &quot;abcdABCD1234&quot;. We read a total of 12 characters from the file, so return 12.
80+
</pre>
81+
82+
<p><strong>Example 4:</strong></p>
83+
84+
<pre>
85+
<strong>Input: </strong>file = &quot;leetcode&quot;, n = 5
86+
<strong>Output: </strong>5
87+
<strong>Explanation: </strong>After calling your read method, buf should contain &quot;leetc&quot;. We read a total of 5 characters from the file, so return 5.
88+
</pre>
89+
90+
<p>&nbsp;</p>
91+
92+
<p><strong>Note:</strong></p>
93+
94+
<ul>
95+
<li>Consider that you <strong>cannot</strong> manipulate the file directly, the file is only accesible for <code>read4</code> but&nbsp;<strong>not</strong> for <code>read</code>.</li>
96+
<li>The <code>read</code> function will only be called once for each test case.</li>
97+
<li>You may assume the destination buffer array,&nbsp;<code>buf</code>,&nbsp;is guaranteed to have enough&nbsp;space for storing&nbsp;<em>n</em>&nbsp;characters.</li>
98+
</ul>
99+
7100

8101

9102
## Solutions

solution/0100-0199/0158.Read N Characters Given Read4 II - Call multiple times/README.md

+62-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,68 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>给你一个文件,并且该文件只能通过给定的&nbsp;<code>read4</code>&nbsp;方法来读取,请实现一个方法使其能够读取 n 个字符。<strong>注意:你的</strong>&nbsp;<strong><code>read</code> 方法可能会被调用多次。</strong></p>
8+
9+
<p><strong>read4 的定义:</strong></p>
10+
11+
<pre>参数类型: char[] buf
12+
返回类型: int
13+
14+
注意: buf[] 是目标缓存区不是源缓存区,read4 的返回结果将会复制到 buf[] 当中。
15+
</pre>
16+
17+
<p>下列是一些使用 <code>read4</code> 的例子:</p>
18+
19+
<pre><code>File file(&quot;abcdefghijk&quot;); // 文件名为 &quot;abcdefghijk&quot;, 初始文件指针 (fp) 指向 &#39;a&#39;
20+
char[] buf = new char[4]; // 创建一个缓存区使其能容纳足够的字符
21+
read4(buf); // read4 返回 4。现在 buf = &quot;abcd&quot;,fp 指向 &#39;e&#39;
22+
read4(buf); // read4 返回 4。现在 buf = &quot;efgh&quot;,fp 指向 &#39;i&#39;
23+
read4(buf); // read4 返回 3。现在 buf = &quot;ijk&quot;,fp 指向文件末尾</code></pre>
24+
25+
<p><strong>read 方法:</strong></p>
26+
27+
<p>通过使用 <code>read4</code> 方法,实现&nbsp;<code>read</code> 方法。该方法可以从文件中读取 n 个字符并将其存储到缓存数组&nbsp;<code>buf</code> 中。您&nbsp;<strong>不能&nbsp;</strong>直接操作文件。</p>
28+
29+
<p>返回值为实际读取的字符。</p>
30+
31+
<p><strong>read&nbsp;的定义:</strong></p>
32+
33+
<pre>参数: char[] buf, int n
34+
返回值: int
35+
36+
注意: buf[] 是目标缓存区不是源缓存区,你需要将结果写入 buf[] 中。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>示例 1:</strong></p>
42+
43+
<pre>File file(&quot;abc&quot;);
44+
Solution sol;
45+
// 假定 buf 已经被分配了内存,并且有足够的空间来存储文件中的所有字符。
46+
sol.read(buf, 1); // 当调用了您的 read 方法后,buf 需要包含 &quot;a&quot;。 一共读取 1 个字符,因此返回 1。
47+
sol.read(buf, 2); // 现在 buf 需要包含 &quot;bc&quot;。一共读取 2 个字符,因此返回 2。
48+
sol.read(buf, 1); // 由于已经到达了文件末尾,没有更多的字符可以读取,因此返回 0。
49+
</pre>
50+
51+
<p><strong>Example 2:</strong></p>
52+
53+
<pre>File file(&quot;abc&quot;);
54+
Solution sol;
55+
sol.read(buf, 4); // 当调用了您的 read 方法后,buf 需要包含 &quot;abc&quot;。 一共只能读取 3 个字符,因此返回 3。
56+
sol.read(buf, 1); // 由于已经到达了文件末尾,没有更多的字符可以读取,因此返回 0。
57+
</pre>
58+
59+
<p><strong>注意:</strong></p>
60+
61+
<ol>
62+
<li>你 <strong>不能</strong> 直接操作该文件,文件只能通过 <code>read4</code> 获取而 <strong>不能</strong> 通过 <code>read</code>。</li>
63+
<li><code>read</code>&nbsp; 函数可以被调用&nbsp;<strong>多次</strong>。</li>
64+
<li>请记得&nbsp;<strong>重置&nbsp;</strong>在 Solution 中声明的类变量(静态变量),因为类变量会&nbsp;<strong>在多个测试用例中保持不变</strong>,影响判题准确。请 <a href="https://support.leetcode-cn.com/hc/kb/section/1071534/" target="_blank">查阅</a> 这里。</li>
65+
<li>你可以假定目标缓存数组&nbsp;<code>buf</code> 保证有足够的空间存下 n 个字符。&nbsp;</li>
66+
<li>保证在一个给定测试用例中,<code>read</code> 函数使用的是同一个 <code>buf</code>。</li>
67+
</ol>
68+
869

970

1071
## 解法

0 commit comments

Comments
 (0)