-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path3622.read-n-characters-given-read4.cpp
130 lines (120 loc) · 2.68 KB
/
3622.read-n-characters-given-read4.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Tag: Simulation
// Time: O(N)
// Space: O(1)
// Ref: Leetcode-157
// Note: -
// In this question, you need to design the `read()` function to read and save the first n characters of the file into `buf`, and return the length of the read string.
//
// You cannot access the file directly, you need to read the file indirectly through the `read4()` function.
// Among them, the `read4()` function reads 4 characters at a time, and if it is called multiple times, it will continue to read from the last read result.
// Similarly, this function will also return the length of the string actually read.
//
// You can see more explanation in the Example.
//
// **Example 1**
//
// Input:
//
// ```plaintext
// "lintcode"
// 5
// ```
//
// Output:
//
// ```plaintext
// 5
// lintc
// ```
//
// Explanation:
//
// The content of file is **lintcode**, the first call to `read4()` reads **lint**, and the second call only needs to read the fifth character (**c**). So the final read string is **lintc** with a length of 5.
//
// **Example 2**
//
// Input:
//
// ```plaintext
// "lintcode"
// 9
// ```
//
// Output:
//
// ```plaintext
// 8
// lintcode
// ```
//
// **Example 3**
//
// Input:
//
// ```plaintext
// "lintcode"
// 0
// ```
//
// Output:
//
// ```plaintext
// 0
// ""
// ```
// Note: `""` is actually an empty string.
//
//
int read4(char* buf);
class Solution {
public:
/**
* @param buf: destination
* @param n: the number of characters that need to be read
* @return: the number of characters read
*/
int read(char* buf, int n) {
// write you code here
int total = 0;
char tmp[4];
while (total < n) {
int count = read4(tmp);
if (count == 0) {
break;
}
int read = min(count, n - total);
for (int i = 0; i < read; i++) {
buf[total + i] = tmp[i];
}
total += read;
}
return total;
}
};
int read4(char* buf);
class Solution {
public:
/**
* @param buf: destination
* @param n: the number of characters that need to be read
* @return: the number of characters read
*/
int read(char* buf, int n) {
// write you code here
char tmp[4];
int offset = 0;
int count = 0;
for(auto i = 0; i < n; ++i) {
if (offset == count) {
count = read4(tmp);
offset = 0;
}
if (offset < count) {
buf[i] = tmp[offset++];
} else {
return i;
}
}
return n;
}
};