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 int read(char *buf, int n) that reads n characters from the file.

Note:
The read function may be called multiple times.

Analysis:

Since the function can be called multiple times, we need to record the left over content in the buffer of read4, and put them in some place. For the next call, we need to read content from the carry over buffer first.

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     /**
 6      * @param buf Destination buffer
 7      * @param n   Maximum number of characters to read
 8      * @return    The number of characters read
 9      */
10
11     public char[] carry;
12     public int index;
13
14     public int read(char[] buf, int n) {
15         int left = n;
16
17         //Put carry over into buf, and destroy the carry over array!
18         if (carry!=null){
19             while (left>0 && index<carry.length){
20                 buf[n-left]=carry[index];
21                 left--;
22                 index++;
23             }
24             if (index>=carry.length){
25                 carry=null;
26                 index=-1;
27             }
28         }
29
30         char[] tempBuf = new char[4];
31         while (left>0){
32             int num = read4(tempBuf);
33             //if the read number is larger then what we need, then we just put the left number of chars into buf.
34             //And put the rest chars into carry array.
35             if (num>left){
36                 carry = new char[num-left];
37                 for (int i=left;i<num;i++)
38                     carry[i-left] = tempBuf[i];
39                 index = 0;
40             }
41
42             int end = Math.min(num,left);
43             for (int i=0;i<end;i++){
44                 buf[n-left] = tempBuf[i];
45                 left--;
46             }
47
48             //If reach EOF.
49             if (left>0 && num<4) break;
50         }
51
52         return n-left;
53
54     }
55 }
时间: 2024-10-01 08:55:26

Leetcode-Read N Characters Given Read4 II的相关文章

[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

[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

/* 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]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

[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 - 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

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