加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode158. 用Read4来读取N个字符II $ Read N Characte

发布时间:2020-12-14 05:09:21 所属栏目:百科 来源:网络整理
导读: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

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.


API:int Read 4(char *BUF)每次从一个文件读取4个字符。

返回值是读取的实际字符数。例如,如果文件中只剩下3个字符,则返回3。

通过使用read4 API,实现从文件中读取n个字符的int read(char*buf,int n)函数。

注:

读取函数可以被多次调用。


 1 class Solution {
 2     var readPos:Int = 0
 3     var writePos:Int = 0
 4     var buff:[Character] = [Character](repeating:" ",count:4)
 5     func read(_ buf:[Character],_ n:Int) -> Int {
 6         var i:Int = 0
 7          while (i < n && (readPos < writePos || (readPos = 0) < (writePos = read4(buff))))
 8         {
 9             buf[i] = buff[readPos]
10             i += 1
11             readPos += 1
12         }
13         return i
14     }
15 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读