LeetCode-Read N Characters Given Read4 II - Call multiple times

The API: int read4(char *buf) reads 4 characters at a time from a file.

The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.

By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

Note:
The read function may be called multiple times.

Solution:

 1 /* The read4 API is defined in the parent class Reader4.
 2       int read4(char[] buf); */
 3
 4 public class Solution extends Reader4 {
 5     char[] tempBuf = new char[4];
 6     int offset = 4;
 7     int maxLen = 4;
 8
 9     /**
10      * @param buf
11      *            Destination buffer
12      * @param n
13      *            Maximum number of characters to read
14      * @return The number of characters read
15      */
16     public int read(char[] buf, int n) {
17         int ind = 0;
18         while (offset < maxLen && ind < n) {
19             buf[ind++] = tempBuf[offset++];
20         }
21         if (maxLen < 4) return ind;
22
23         ind = readFile(buf, ind, n);
24         return ind;
25     }
26
27     public int readFile(char[] buf, int ind, int n) {
28         int num = 4;
29         while (num == 4 && ind < n) {
30             num = read4(tempBuf);
31             int end = Math.min(num, n - ind);
32             for (offset = 0; offset < end; offset++) {
33                 buf[ind++] = tempBuf[offset];
34             }
35         }
36         maxLen = num;
37         return ind;
38     }
39 }
时间: 2024-12-17 05:02:36

LeetCode-Read N Characters Given Read4 II - Call multiple times的相关文章

[Locked] Read N Characters Given Read4 &amp; Read N Characters Given Read4 II - Call multiple times

Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the rea

[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function

leetcode 160: Read N Characters Given Read4 II - Call multiple times

Total Accepted: 909 Total Submissions: 4757 The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By

[leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function

Read N Characters Given Read4 II - Call multiple times

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function

[LeetCode] Read N Characters Given Read4 I &amp; II

Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the rea

LeetCode 158: Read N Characters Given Read4 II

/* The read4 API is defined in the parent class Reader4. int read4(char[] buf); */ public class Solution extends Reader4 { /** * @param buf Destination buffer * @param n Maximum number of characters to read * @return The number of characters read */

[leetcode]Read N Characters Given Read4

用read4实现readn... 至调用一次,感觉怎么搞都可以...估计这个题有II就是调用多次了... 感觉多次勇哥buffer存下多读的那部分就好了... // Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** * @param buf Destination buffer * @param n Maximum number of characters to rea

Leetcode-Read N Characters Given Read4 II

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the read4 API, implement the function