w*****r 发帖数: 42 | 1 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.
有人做了吗?第一次读多了以后怎么把文件指针退回去? 还是有别的办法?我用原来
那个读一次的代码试了一下,过不了下面这个testcase.
Input: "ab", [read(1),read(2)]
Output: ["a",""]
Expected: ["a","b"] | c*******e 发帖数: 621 | 2 不能退 暂时存着
【在 w*****r 的大作中提到】 : 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. : 有人做了吗?第一次读多了以后怎么把文件指针退回去? 还是有别的办法?我用原来 : 那个读一次的代码试了一下,过不了下面这个testcase.
| c*******e 发帖数: 621 | 3 代码在此
http://blog.csdn.net/a83610312/article/details/12872437
注意理解那个static variable
话说这本来是一道题 leetcode硬是拆成了两道。。。 | s***c 发帖数: 639 | 4 试了能过你那个case
char buf4[4] = {0};
int b4left = 0;
char *pa = buf4;
int read(char *buf, int n){
if (n <= b4left){
memcpy(buf, pa, n);
b4left -= n;
pa += n;
buf += n;
return n;
}
else{
memcpy(buf, pa, b4left);
n -= b4left;
buf += b4left;
}
int npre = b4left;
b4left = 0;
pa = buf4;
char *ptr = buf;
int cnt = 0, nrd = 4;
while (cnt < n && nrd == 4){
nrd = read4(buf4);
if (cnt + nrd > n){
memcpy(ptr, buf4, n - cnt);
ptr += n - cnt;
pa += n - cnt;
b4left = cnt + nrd - n;
cnt = n;
}
else{
memcpy(ptr, buf4, nrd);
cnt += nrd;
ptr += nrd;
}
}
return cnt + npre;
}
【在 w*****r 的大作中提到】 : 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. : 有人做了吗?第一次读多了以后怎么把文件指针退回去? 还是有别的办法?我用原来 : 那个读一次的代码试了一下,过不了下面这个testcase.
| s******7 发帖数: 1758 | 5 贴个java的
就是把read4多读出来的存起来, 下次先读这段剩余的,refactor了一下,看起来短点
,要是面试直接上多次引用,我第一次碰到半个小时肯定搞不定
private List left;
public int read(char[] buf, int n) {
if(left==null) left = new ArrayList();
int ptr = Math.min(n,left.size());
for(int i=0;i
left.subList(0,ptr).clear();
if(n
else{
while(ptr < n){
char[] b4 = new char[4];
int r = read4(b4);
if(r==0) return ptr;
int min2 = Math.min(r,n-ptr);
for(int i=0;i
if(min2
}
return ptr;
}
} | h*******n 发帖数: 357 | 6 我做了一下,结果给了这个error:
Submission Result: Wrong Answer
Input: "", [read(1)]
Output: [""]
Expected: [""]
这是虾米意思,output和expected的一样啊 | m*****k 发帖数: 731 | 7 while(ptr < n){
char[] b4 = new char[4];
int r = read4(b4);
if(r==0) return ptr;
int min2 = Math.min(r,n-ptr);
for(int i=0;i
if(min2
}
请教一下为何不先处理left中的,而是反复left.add()? |
|