|
3 | 3 | [中文文档](/solution/0100-0199/0157.Read%20N%20Characters%20Given%20Read4/README.md)
|
4 | 4 |
|
5 | 5 | ## Description
|
6 |
| -None |
| 6 | +<p>Given a file and assume that you can only read the file using a given method <code>read4</code>, implement a method to read <em>n</em> characters.</p> |
| 7 | + |
| 8 | +<p> </p> |
| 9 | + |
| 10 | +<p><b>Method read4: </b></p> |
| 11 | + |
| 12 | +<p>The API <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 <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("</code>abcde<code>"); // File is "</code>abcde<code>", initially file pointer (fp) points to 'a' |
| 33 | +char[] buf4 = new char[4]; // Create buffer with enough space to store characters |
| 34 | +read4(buf4); // read4 returns 4. Now buf = "abcd", fp points to 'e' |
| 35 | +read4(buf4); // read4 returns 1. Now buf = "e", fp points to end of file |
| 36 | +read4(buf4); // read4 returns 0. Now buf = "", fp points to end of file</code> |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +<p><strong>Method read:</strong></p> |
| 42 | + |
| 43 | +<p>By using the <code>read4</code> method, implement the method <code>read</code> that reads <i>n</i> characters from the file and store it in the buffer array <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> </p> |
| 57 | + |
| 58 | +<p><strong>Example 1:</strong></p> |
| 59 | + |
| 60 | +<pre> |
| 61 | +<strong>Input: </strong>file = "abc", n = 4 |
| 62 | +<strong>Output: </strong>3 |
| 63 | +<strong>Explanation:</strong> After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. Note that "abc" is the file'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 = "abcde", n = 5 |
| 70 | +<strong>Output: </strong>5 |
| 71 | +<strong>Explanation: </strong>After calling your read method, buf should contain "abcde". 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 = "abcdABCD1234", n = 12 |
| 78 | +<strong>Output: </strong>12 |
| 79 | +<strong>Explanation: </strong>After calling your read method, buf should contain "abcdABCD1234". 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 = "leetcode", n = 5 |
| 86 | +<strong>Output: </strong>5 |
| 87 | +<strong>Explanation: </strong>After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5. |
| 88 | +</pre> |
| 89 | + |
| 90 | +<p> </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 <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, <code>buf</code>, is guaranteed to have enough space for storing <em>n</em> characters.</li> |
| 98 | +</ul> |
| 99 | + |
7 | 100 |
|
8 | 101 |
|
9 | 102 | ## Solutions
|
|
0 commit comments