-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path660.read-n-characters-given-read4-ii-call-multiple-times.cpp
80 lines (76 loc) · 1.7 KB
/
660.read-n-characters-given-read4-ii-call-multiple-times.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Tag: String, Simulation
// Time: O(N)
// Space: O(1)
// Ref: Leetcode-158
// Note: -
// 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.
//
// **Example 1**
//
// ```plain
// Input:
// "filetestbuffer"
// read(6)
// read(5)
// read(4)
// read(3)
// read(2)
// read(1)
// read(10)
// Output:
// 6, buf = "filete"
// 5, buf = "stbuf"
// 3, buf = "fer"
// 0, buf = ""
// 0, buf = ""
// 0, buf = ""
// 0, buf = ""
// ```
//
// **Example 2**
//
// ```plain
// Input:
// "abcdef"
// read(1)
// read(5)
// Output:
// 1, buf = "a"
// 5, buf = "bcdef"
// ```
//
// The `read` function may be called multiple times.
// Forward declaration of the read4 API.
int read4(char *buf);
class Solution {
public:
/**
* @param buf destination buffer
* @param n maximum number of characters to read
* @return the number of characters read
*/
int count = 0;
int offset = 0;
char tmp[4];
int read(char *buf, int n) {
// Write your code here
int length = 0;
while (length < n) {
if (count == offset) {
count = read4(tmp);
offset = 0;
}
if (offset < count) {
buf[length++] = tmp[offset++];
} else {
break;
}
}
return length;
}
};