f******4 发帖数: 51 | 1 FG以前都面过的题目,貌似出现概率不低。搜索+论坛考古之后实在没有研究出满意的
答案,
原题如下:
Given API:
int Read4096(char* buf);
It reads data from a file and records the position so that the next time
when it is called it reads the next 4k chars (or the rest of the file,
whichever is smaller) from the file.
The return is the number of chars read.
Todo: Use above API to Implement API
"int Read(char* buf, int n)" which reads any number of chars from the file.
有没有大牛甩个python解法 |
T*****u 发帖数: 7103 | 2 说说思路?是不是搞一个x<4096,读ceiling(n/4096)次,扔出前n个,留下剩下的在x
,下次读的时候从x继续? |
s**x 发帖数: 7506 | |
f******4 发帖数: 51 | |
q********c 发帖数: 1774 | 5 这道题挺有意思,不难但是要考虑几种情况, 试了一下:
int Read(char* buf, int n)
{
int total = 0;
while(true) {
total += Read4096(buf);
if(total == n || total < 4096) { break; }
if(total > n) {
total = n; buf += total;
break;
}
}
return total;
} |
f******4 发帖数: 51 | 6
buf是Input param, buf += total显然有问题a
【在 q********c 的大作中提到】 : 这道题挺有意思,不难但是要考虑几种情况, 试了一下: : int Read(char* buf, int n) : { : int total = 0; : while(true) { : total += Read4096(buf); : if(total == n || total < 4096) { break; } : if(total > n) { : total = n; buf += total; : break;
|
a********e 发帖数: 53 | |
y***n 发帖数: 1594 | 8 这个题是很容易把我这种文科生转行的打回原形的。还有一个UTF8啥的,也是类似。 |
p***y 发帖数: 637 | 9 read(buf,1)
total=""> n) {
【在 q********c 的大作中提到】 : 这道题挺有意思,不难但是要考虑几种情况, 试了一下: : int Read(char* buf, int n) : { : int total = 0; : while(true) { : total += Read4096(buf); : if(total == n || total < 4096) { break; } : if(total > n) { : total = n; buf += total; : break;
|
f******4 发帖数: 51 | 10 FG以前都面过的题目,貌似出现概率不低。搜索+论坛考古之后实在没有研究出满意的
答案,
原题如下:
Given API:
int Read4096(char* buf);
It reads data from a file and records the position so that the next time
when it is called it reads the next 4k chars (or the rest of the file,
whichever is smaller) from the file.
The return is the number of chars read.
Todo: Use above API to Implement API
"int Read(char* buf, int n)" which reads any number of chars from the file.
有没有大牛甩个python解法 |
|
|
T*****u 发帖数: 7103 | 11 说说思路?是不是搞一个x<4096,读ceiling(n/4096)次,扔出前n个,留下剩下的在x
,下次读的时候从x继续? |
s**x 发帖数: 7506 | |
f******4 发帖数: 51 | |
q********c 发帖数: 1774 | 14 这道题挺有意思,不难但是要考虑几种情况, 试了一下:
int Read(char* buf, int n)
{
int total = 0;
while(true) {
total += Read4096(buf);
if(total == n || total < 4096) { break; }
if(total > n) {
total = n; buf += total;
break;
}
}
return total;
} |
f******4 发帖数: 51 | 15
buf是Input param, buf += total显然有问题a
【在 q********c 的大作中提到】 : 这道题挺有意思,不难但是要考虑几种情况, 试了一下: : int Read(char* buf, int n) : { : int total = 0; : while(true) { : total += Read4096(buf); : if(total == n || total < 4096) { break; } : if(total > n) { : total = n; buf += total; : break;
|
a********e 发帖数: 53 | |
y***n 发帖数: 1594 | 17 这个题是很容易把我这种文科生转行的打回原形的。还有一个UTF8啥的,也是类似。 |
p***y 发帖数: 637 | 18 read(buf,1)
total=""> n) {
【在 q********c 的大作中提到】 : 这道题挺有意思,不难但是要考虑几种情况, 试了一下: : int Read(char* buf, int n) : { : int total = 0; : while(true) { : total += Read4096(buf); : if(total == n || total < 4096) { break; } : if(total > n) { : total = n; buf += total; : break;
|
L*****1 发帖数: 34 | |
r*******e 发帖数: 971 | 20 有,看我的。
public class Solution extends Reader4k {
private int offset;
private int size;
private char[] buffer;
public Solution(){
offset=0;
size=0;
buffer = new char[4096];
}
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] target, int n) {
int readbytes=0,step=0;
boolean isEnd = false;
while(!isEnd&&readbytes
//1. Check if invoking reader4k is needed && if the end of the
file is reached
if (size==0) {size=read4k(buffer); isEnd = size!=4096;}
//2. Calculate how many chars need to be copied
step = Math.min(size,n-readbytes);
//3. Copy
System.arraycopy(buffer,offset,target,readbytes,step);
//4. Incremet counters
readbytes+=step;
offset=(step+offset)%4096;
size-=step;
}
return readbytes;
}
} |
r*******e 发帖数: 971 | 21 发了
【在 L*****1 的大作中提到】 : 求教这道题有java版本解法嘛?谢谢
|