-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path157*.cpp
More file actions
23 lines (21 loc) · 682 Bytes
/
Copy path157*.cpp
File metadata and controls
23 lines (21 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 157. Read N Characters Given Read4 - https://leetcode.com/problems/read-n-characters-given-read4
// Forward declaration of the read4 API.
int read4(char *buf);
// It assumes only read less than n chars,
// even if the there are more than n chars in a file.
class Solution {
public:
int read(char *buf, int n) {
int total = 0;
// 10 = 4 * (10 / 4 == 2) + 2
for (int i = 0; i < n / 4; i++)
total += read4(buf + total);
int remain = n - total;
if (remain) {
// 9 -> read = 1, remain = 2
int read = read4(buf + total);
return total + min(read, remain);
}
return n;
}
};