[Swift]LeetCode157.用Read4來讀取N個字符 $ Read N Characters Given Read4

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rfnfsjqy-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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 will only be called once for each test case.git


API:int Read 4(char *BUF)每次從一個文件讀取4個字符。github

返回值是讀取的實際字符數。例如,若是文件中只剩下3個字符,則返回3。微信

經過使用read4 API,實現從文件中讀取n個字符的int read(char*buf,int n)函數。函數

注:測試

讀取函數只對每一個測試用例調用一次。spa


 1 class Solution {
 2     func read(_ buf:[Character],_ n:Int) -> Int {
 3         var res:Int = 0
 4         for i in 0...n
 5         {
 6             var cur:Int = read4(buf + res)
 7             if cur == 0 {break}
 8             res += cur
 9         }
10         return min(res, n)
11     }
12 }
相關文章
相關標籤/搜索