Read N characters Given Read4Total Accepted:
960 Total Submissions:
3775
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 are only 3 characters left in the file.
read4
by using the API, implement the function that int read(char *buf, int n)
readsn characters from the file.
Note:
The function is only being read
called once for each test case.
Analysis
None
Note
None
[CODE]
/* The READ4 API is defined in the parent class Reader4. int Read4 (char[] buf); */public class Solution extends Reader4 { /** * @param buf Destination Buffer * @param n Maximum number of Characters to read * @return The number of characters read * /public int read (char[] buf, int n) { if (Buf==null | | n<=0) return 0; int i=0; char[] temp = new Char[4]; while (n>0) { int x = READ4 (temp); int j = 0; int k = x; while (n>0 && x>0) { buf[i++] = temp[j++]; --x; --n; } if (k<4) break; } return i; } }
Leetcode 157:read N characters Given Read4