标签:
Well, since I have no access to this problem, I only obtain it from the internet. My understanding of this problem is to return the actual number of characters that we read from char* buf
if we try to read n
characters. So I guess the following 1-line code is OK :)
1 int read(char *buf, int n) { 2 return min(n, (int)strlen(buf)); 3 }
However, I think the key to this problem is to avoid using any built-in function. Specifically, we need to obtain the actual number of characters read using the provided API read4
.
Well, this problem is not quite difficult. But it is also not easy to give a clear explanation to it. I will try my best to explain it in the following.
To read n
characters, we call read4
for n / 4
times. Intuitively, if we want to read 10
characters, we read them in the 8(4 + 4) + 2
manner by calling read4
for n / 4
times to read the 8
characters first.
Then we see if we have any remaining number of characters to read (in this case, remain = 2
).
If remain > 0
, we read them again using read4
. However, we may not be able to read all of them. For example, buf
has 9
characters and we need to read 10
. After reading the 8
characters we can only read the remining 1
character. In this case, we simply add the minimum of remain
and the actual number of characters read by read4
to the total count and return it.
Otherwise, we are done and just return n
.
Putting these together, we have the following code.
1 /** 2 The API: int read4(char *buf) reads 4 characters at a time from a file. 3 The return value is the actual number of characters read. For example, it returns 3 4 if there is only 3 characters left in the file. 5 By using the read4 API, implement the function int read(char *buf, int n) that reads 6 n characters from the file. 7 Note: 8 The read function will only be called once for each test case. 9 */ 10 11 #include <iostream> 12 13 using namespace std; 14 15 int read4(char* buf) { 16 int count = 0; 17 for (int i = 0; buf[i] && count < 4; i++) 18 count++; 19 return count; 20 } 21 22 void read4Test(void) { 23 printf("Computed: %d, Expected: %d.\n", read4("LeetCode"), min(4, (int)strlen("hello"))); 24 printf("Computed: %d, Expected: %d.\n", read4("Test"), min(4, (int)strlen("test"))); 25 printf("Computed: %d, Expected: %d.\n", read4("AC"), min(4, (int)strlen("hi"))); 26 } 27 28 class Solution { 29 public: 30 int read(char *buf, int n) { 31 int count = 0; 32 for (int i = 0; i < n / 4; i++) { 33 int move = read4(buf + count); 34 count += move; 35 } 36 int remain = n - count; 37 if (remain) { 38 int move = read4(buf + count); 39 return count + min(move, remain); 40 } 41 return n; 42 } 43 }; 44 45 void readTest(void) { 46 Solution sol; 47 // String of length being multiples of 4 48 for (int n = 0; n <= 20; n++) { 49 char str[] = "TopCoder"; 50 int nRead = sol.read(str, n); 51 printf("Read %d characters from %s: %d are read, ", n, str, nRead); 52 for (int i = 0; i < nRead; i++) 53 printf("%c", str[i]); 54 printf("\n"); 55 } 56 // String of length being multiples of 4 (less than 4) 57 for (int n = 0; n <= 20; n++) { 58 char str[] = "GJC"; 59 int nRead = sol.read(str, n); 60 printf("Read %d characters from %s: %d are read, ", n, str, nRead); 61 for (int i = 0; i < nRead; i++) 62 printf("%c", str[i]); 63 printf("\n"); 64 } 65 // String of length not being multiples of 4 (greater than 4) 66 for (int n = 0; n <= 20; n++) { 67 char str[] = "CodeForces"; 68 int nRead = sol.read(str, n); 69 printf("Read %d characters from %s: %d are read, ", n, str, nRead); 70 for (int i = 0; i < nRead; i++) 71 printf("%c", str[i]); 72 printf("\n"); 73 } 74 } 75 76 int main(void) { 77 read4Test(); 78 readTest(); 79 system("pause"); 80 return 0; 81 }
If you run this code, you are expected to obtain (I run it in Microsoft Visual Studio Professional 2012):
Computed: 4, Expected: 4. Computed: 4, Expected: 4. Computed: 2, Expected: 2. Read 0 characters from TopCoder: 0 are read, Read 1 characters from TopCoder: 1 are read, T Read 2 characters from TopCoder: 2 are read, To Read 3 characters from TopCoder: 3 are read, Top Read 4 characters from TopCoder: 4 are read, TopC Read 5 characters from TopCoder: 5 are read, TopCo Read 6 characters from TopCoder: 6 are read, TopCod Read 7 characters from TopCoder: 7 are read, TopCode Read 8 characters from TopCoder: 8 are read, TopCoder Read 9 characters from TopCoder: 8 are read, TopCoder Read 10 characters from TopCoder: 8 are read, TopCoder Read 11 characters from TopCoder: 8 are read, TopCoder Read 12 characters from TopCoder: 8 are read, TopCoder Read 13 characters from TopCoder: 8 are read, TopCoder Read 14 characters from TopCoder: 8 are read, TopCoder Read 15 characters from TopCoder: 8 are read, TopCoder Read 16 characters from TopCoder: 8 are read, TopCoder Read 17 characters from TopCoder: 8 are read, TopCoder Read 18 characters from TopCoder: 8 are read, TopCoder Read 19 characters from TopCoder: 8 are read, TopCoder Read 20 characters from TopCoder: 8 are read, TopCoder Read 0 characters from GCJ: 0 are read, Read 1 characters from GCJ: 1 are read, G Read 2 characters from GCJ: 2 are read, GC Read 3 characters from GCJ: 3 are read, GCJ Read 4 characters from GCJ: 3 are read, GCJ Read 5 characters from GCJ: 3 are read, GCJ Read 6 characters from GCJ: 3 are read, GCJ Read 7 characters from GCJ: 3 are read, GCJ Read 8 characters from GCJ: 3 are read, GCJ Read 9 characters from GCJ: 3 are read, GCJ Read 10 characters from GCJ: 3 are read, GCJ Read 11 characters from GCJ: 3 are read, GCJ Read 12 characters from GCJ: 3 are read, GCJ Read 13 characters from GCJ: 3 are read, GCJ Read 14 characters from GCJ: 3 are read, GCJ Read 15 characters from GCJ: 3 are read, GCJ Read 16 characters from GCJ: 3 are read, GCJ Read 17 characters from GCJ: 3 are read, GCJ Read 18 characters from GCJ: 3 are read, GCJ Read 19 characters from GCJ: 3 are read, GCJ Read 20 characters from GCJ: 3 are read, GCJ Read 0 characters from CodeForces: 0 are read, Read 1 characters from CodeForces: 1 are read, C Read 2 characters from CodeForces: 2 are read, Co Read 3 characters from CodeForces: 3 are read, Cod Read 4 characters from CodeForces: 4 are read, Code Read 5 characters from CodeForces: 5 are read, CodeF Read 6 characters from CodeForces: 6 are read, CodeFo Read 7 characters from CodeForces: 7 are read, CodeFor Read 8 characters from CodeForces: 8 are read, CodeForc Read 9 characters from CodeForces: 9 are read, CodeForce Read 10 characters from CodeForces: 10 are read, CodeForces Read 11 characters from CodeForces: 10 are read, CodeForces Read 12 characters from CodeForces: 10 are read, CodeForces Read 13 characters from CodeForces: 10 are read, CodeForces Read 14 characters from CodeForces: 10 are read, CodeForces Read 15 characters from CodeForces: 10 are read, CodeForces Read 16 characters from CodeForces: 10 are read, CodeForces Read 17 characters from CodeForces: 10 are read, CodeForces Read 18 characters from CodeForces: 10 are read, CodeForces Read 19 characters from CodeForces: 10 are read, CodeForces Read 20 characters from CodeForces: 10 are read, CodeForces
Welcome for any question, comment and suggestion about the code!
[LeetCode] Read N Characters Given Read4
标签:
原文地址:http://www.cnblogs.com/jcliBlogger/p/4554948.html