/**
* The read4 API is defined in the parent class Reader4.
* int read4(char *buf4);
*/
class Solution {
public:
/**
* @param buf Destination buffer
* @param n Number of characters to read
* @return The number of actual characters read
*/
int read(char *buf, int n) {
bool flag = true;
int length=0;
while(flag){
char buffer[4];
int m = read4(buffer);
if(m+length>n){
for(int i=length,j=0;i<n;i++,j++){
buf[i]=buffer[j];
}
length = n;
break;
}
if(m==4){
for(int i=0;i<4;i++){
buf[i+length]=buffer[i];
}
length+=4;
continue;
}
else{
for(int i=0;i<m;i++){
buf[i+length]=buffer[i];
}
length+=m;
break;
}
}
buf[length]='\0';
return length;
}
};
157. 用 Read4 读取 N 个字符
最新推荐文章于 2024-09-16 06:54:08 发布