LeetCode 158: Read N Characters Given read4 II - Call Multiple Times Solution

Master LeetCode problem 158 (Read N Characters Given read4 II - Call Multiple Times), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

158. Read N Characters Given read4 II - Call Multiple Times

Problem Explanation

Explanation

To solve this problem, we need to implement a method int read(char[] buf, int n) that reads data from a file using the read4 API multiple times. The read4 API reads 4 characters at a time and returns the number of characters read. The read method should return the total number of characters read.

We can maintain an internal buffer to store the characters read by read4 but not yet consumed by the read method. We use two pointers bufPtr and bufCnt to keep track of the current position in the buffer and the total number of characters in the buffer respectively.

The algorithm works as follows:

  1. If bufPtr reaches the end of the buffer, we read more characters from the file using read4 and refill the buffer.
  2. We copy characters from the internal buffer to the output buffer buf until either we reach n characters or we exhaust all characters in the internal buffer.
  3. If we reach the end of the file or n characters are read, we return the total number of characters read.

Solution Code

public class Solution extends Reader4 {
    private char[] buffer = new char[4];
    private int bufPtr = 0;
    private int bufCnt = 0;

    public int read(char[] buf, int n) {
        int total = 0;
        boolean eof = false;

        while (!eof && total < n) {
            if (bufPtr == 0) {
                bufCnt = read4(buffer);
            }

            if (bufCnt == 0) {
                eof = true;
            }

            while (total < n && bufPtr < bufCnt) {
                buf[total++] = buffer[bufPtr++];
            }

            if (bufPtr == bufCnt) {
                bufPtr = 0;
            }
        }

        return total;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 158 (Read N Characters Given read4 II - Call Multiple Times)?

This page provides optimized solutions for LeetCode problem 158 (Read N Characters Given read4 II - Call Multiple Times) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 158 (Read N Characters Given read4 II - Call Multiple Times)?

The time complexity for LeetCode 158 (Read N Characters Given read4 II - Call Multiple Times) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 158 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 158 in Java, C++, or Python.

Back to LeetCode Solutions