Read N Characters Given Read4

原创 2015年07月08日 23:46:57

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.

/* The read4 API is defined in the parent class Reader4.
      int read4(char[] buf); */

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    public int read(char[] buf, int n) {
        char[] buffer = new char[4];
        boolean eof = false;
        int read = 0;//number of characters has been read
        
        while(!eof && read < n) {
            int count = read4(buffer);
            int remain = Math.min(count, n - read);
            
            if (count < 4)
                eof = true;
            
            System.arraycopy(buffer, 0, buf, read, remain);
            
            read = read + remain;
        }
        
        return read;
    }
}




Leetcode 157 & 158 Read N Characters Given Read4 I & II - Call multiple times

157. Read N Characters Given Read4 Total Accepted: 17410 Total Submissions: 58870 Difficulty: Easy ...
  • fantasiasango
  • fantasiasango
  • 2016年09月26日 13:58
  • 2578

[LeetCode] 157. Read N Characters Given Read4

原题链接:https://leetcode.com/problems/read-n-characters-given-read4/ 这也是一道带锁题,题目的意思是给定一个函数read4(char[] ...
  • foolnote
  • foolnote
  • 2016年02月02日 07:21
  • 520

Read N Characters Given Read4

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the act...
  • u013325815
  • u013325815
  • 2014年12月12日 12:09
  • 4337

Read N Characters Given Read4 II - Call multiple times

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the act...
  • u013325815
  • u013325815
  • 2014年12月12日 13:14
  • 2981

leetcode 157: Read N Characters Given Read4

Read N Characters Given Read4 Total Accepted: 960 Total Submissions: 3775 The API: int read4(char...
  • xudli
  • xudli
  • 2015年01月06日 14:30
  • 4086

leetcode 160: Read N Characters Given Read4 II - Call multiple times

Total Accepted: 909 Total Submissions: 4757 The API: int read4(char *buf) reads 4 charact...
  • xudli
  • xudli
  • 2015年01月28日 08:35
  • 1812

LeetCode 158. Read N Characters Given Read4 II - Call multiple times(读文件流)

原题网址:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ The API: ...
  • jmspan
  • jmspan
  • 2016年05月26日 00:47
  • 720

LeetCode 157. Read N Characters Given Read4

The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the act...
  • xinqrs01
  • xinqrs01
  • 2017年01月05日 15:33
  • 50

leetcode Read N Characters Given Read4

// Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** ...
  • gunxueqiucjw
  • gunxueqiucjw
  • 2014年12月22日 13:51
  • 881

[leetcode 157] Read N Characters Given Read4

Question: The API: int read4(char *buf) reads 4 characters at a time from a file. The return...
  • Xiaohei00000
  • Xiaohei00000
  • 2015年11月06日 14:04
  • 778
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Read N Characters Given Read4
举报原因:
原因补充:

(最多只允许输入30个字)