由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - FG题目包子求教--read4096
相关主题
贡献一个G家面试题贡献一道G家的面试题
问 Implement readline using read4096求问下面这几行代码是做什么的,非常感谢!
fb面试题【转】回馈本版,贴GOOGLE电话面经
leetcode 的新题好像不太理解题意fb面试题【转】
昨天的F家店面问个面试时候hash table的C++实现问题
能有人稍微给我解释下read4 java的leetcode给出的解法吗?半懂思科的电面,还有设计题,大牛帮看看
MS interview questionImplement strStr() ?
find k missing numbers in range [0, N].这道雅虎的面试题绝了,有谁会做吗
相关话题的讨论汇总
话题: buf话题: total话题: int话题: read话题: readbytes
进入JobHunting版参与讨论
1 (共1页)
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
3
以前有人贴过,代码很简单,思路楼上的。
f******4
发帖数: 51
4
这个链接一楼的答案难道想多了? 这题感觉没有那么容易啊
http://www.careercup.com/question?id=14424684
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
7
原来那个简洁版的代码在哪呀。。。。
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解法
相关主题
能有人稍微给我解释下read4 java的leetcode给出的解法吗?半懂贡献一道G家的面试题
MS interview question求问下面这几行代码是做什么的,非常感谢!
find k missing numbers in range [0, N].回馈本版,贴GOOGLE电话面经
进入JobHunting版参与讨论
T*****u
发帖数: 7103
11
说说思路?是不是搞一个x<4096,读ceiling(n/4096)次,扔出前n个,留下剩下的在x
,下次读的时候从x继续?
s**x
发帖数: 7506
12
以前有人贴过,代码很简单,思路楼上的。
f******4
发帖数: 51
13
这个链接一楼的答案难道想多了? 这题感觉没有那么容易啊
http://www.careercup.com/question?id=14424684
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
16
原来那个简洁版的代码在哪呀。。。。
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
19
求教这道题有java版本解法嘛?谢谢
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版本解法嘛?谢谢
1 (共1页)
进入JobHunting版参与讨论
相关主题
这道雅虎的面试题绝了,有谁会做吗昨天的F家店面
GOOG intern interview 题目能有人稍微给我解释下read4 java的leetcode给出的解法吗?半懂
【一个BB公司问的字母排序的问题】MS interview question
问个《编程实践》(英文版)里面的问题find k missing numbers in range [0, N].
贡献一个G家面试题贡献一道G家的面试题
问 Implement readline using read4096求问下面这几行代码是做什么的,非常感谢!
fb面试题【转】回馈本版,贴GOOGLE电话面经
leetcode 的新题好像不太理解题意fb面试题【转】
相关话题的讨论汇总
话题: buf话题: total话题: int话题: read话题: readbytes