Redian新闻
>
leetcode 的新题好像不太理解题意
avatar
leetcode 的新题好像不太理解题意# JobHunting - 待字闺中
w*r
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"]
avatar
c*e
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.

avatar
s*c
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.

avatar
s*7
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;ileft.subList(0,ptr).clear();
if(nelse{
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;iif(min2}
return ptr;
}
}
avatar
h*n
6
我做了一下,结果给了这个error:
Submission Result: Wrong Answer
Input: "", [read(1)]
Output: [""]
Expected: [""]
这是虾米意思,output和expected的一样啊
avatar
m*k
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;iif(min2}
请教一下为何不先处理left中的,而是反复left.add()?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。