2 minute read

For this problem I am given a method that reads 4 characters from a file and I have to implement a method that will read n characters from a file using this method. The characters are copied into the buffer ‘buf’ and I must return the number of characters copied.
URL: https://leetcode.com/problems/read-n-characters-given-read4/submissions/

My solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * The read4 API is defined in the parent class Reader4.
 *     int read4(char[] buf4);
 */

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return    The number of actual characters read
     */
    public int read(char[] buf, int n) {
        int cnt = 0;
        int bufIdx = 0;
        char[] arr = new char[4];
        while (n > 0) {
            
            int num = read4(arr); // r is the number of characters read
            
            if (num == 0) { //ran out of the characters in the file exit loop
                return cnt;
            }
            
            if (n >= 4) { //more than 4 characters remaining to read
                for (int i = 0; i < num; i++) { //copy characters into buff array
                    buf[bufIdx] = arr[i];
                    cnt++;
                    bufIdx++;
                } 
            }
            else { //there are less than 4 chars remaining to read
               int min = Math.min(n,num); //if num is less than n, then there are not enough characters in file to read. If num is greater than n, then there are enough characters left in the file to read
                for (int i = 0; i < min; i++) { //copy characters into buff array
                    buf[bufIdx] = arr[i];
                    cnt++;
                    bufIdx++;
                }                 
            }
            n = n - num; //decrement the amount of characters remaining to be read
        }

        return cnt;
    }
}

Explanation:
Line 13 initialized the variable ‘cnt’ which will count the number of characters that were written into the buffer.

Line 15 initializes the array ‘arr’ that will be used to pass into the read4 method.

Line 16 is the loop condition. Before the loop condition evaluates, ‘n’ is the amount of character remaining to be read. At the end loop, n will be decremented by the amount of characters copied into the buffer. The loop then revaluates the condition.

Line 18 executes the read4 method and stores the characters into ‘arr’ and returns the amount of characters read into the variable ‘num’.

Line 20. If ‘num’ is equal to 0, then there are no more characters available to count and therefore returns the count.

Line 24 checks if the number of characters remaining is more than 4, if so then a procedure is run to copy the characters into the buffer from ‘arr’ and count the characters copied.

Line 31 executes if the number of characters is less than 4, if so then the amount of characters that will be written must be the minimum of ‘n’ or ‘num’. The reasoning is that if ‘num’ is greater than ‘n’, then there are more characters in the file that are required to read and so ‘n’ is chosen. If ‘n’ is greater than ‘num’, then there are more characters that are required to be read than there are in the file and so ‘num’ is chosen. The copy procedure to copy and count characters is then executed.

Line 39 decrements the amount of characters copied from n.

Line 42 returns the amount of characters copied.