-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path158. Read N Characters Given Read4 II - Call multiple times.c
83 lines (66 loc) · 1.95 KB
/
158. Read N Characters Given Read4 II - Call multiple times.c
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
81
82
83
/*
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 int read(char *buf, int n) that reads n characters from the file.
Note:
The read function may be called multiple times.
*/
// Forward declaration of the read4 API.
int read4(char *buf);
class Solution {
private:
char saved[4];
int saved_start = 0;
int saved_len = 0;
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
char *p = buf;
int i, l, k = 0;
if (!n) return n;
if (saved_len) {
k = n < saved_len ? n : saved_len;
memcpy(p, &saved[saved_start], k * sizeof(char));
saved_start = (saved_start + k) % 4;
saved_len -= k;
p += k;
n -= k;
}
while (n >= 4) {
l = read4(p);
k += l;
if (l < 4) return k;
p += 4;
n -= 4;
}
if (n) {
l = read4(saved);
i = l < n ? l : n;
memcpy(p, saved, i * sizeof(char));
saved_start = i;
saved_len = l - i;
k += i;
}
return k;
}
};
/*
Difficulty:Hard
Total Accepted:27.8K
Total Submissions:115.4K
Companies Bloomberg Google Facebook
Related Topics String
Similar Questions
Read N Characters Given Read4
*/