1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
read-n-characters-given-read4-ii-call-multiple-times.cpp 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(n)
// Space: O(1)
// Forward declaration of the read4 API.
int read4(char *buf);
// shorter version of Solution2
class Solution {
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
int i = 0;
while (i < n && (i4_ < n4_ || (i4_ = 0) < (n4_ = read4(buf4_)))) {
buf[i++] = buf4_[i4_++];
}
return i;
}
private:
char buf4_[4];
int i4_ = 0, n4_ = 0;
};
// Time: O(n)
// Space: O(1)
class Solution2 {
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
int i = 0;
while (i < n) {
if (i4_ < n4_) { // Any characters in buf4.
buf[i++] = buf4_[i4_++];
} else if (n4_ = read4(buf4_)) { // Read more characters.
i4_ = 0;
} else { // Buffer has been empty.
break;
}
}
return i;
}
private:
char buf4_[4];
int i4_ = 0, n4_ = 0;
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助