Skip to content

Commit 90b503d

Browse files
refactor 158
1 parent 1a1a9fb commit 90b503d

File tree

1 file changed

+49
-34
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+49
-34
lines changed
Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,63 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* The API: int read4(char *buf) reads 4 characters at a time from a file.
4+
158. Read N Characters Given Read4 II - Call multiple times
5+
The API: int read4(char *buf) reads 4 characters at a time from a file.
56
67
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.
78
89
By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
910
1011
Note:
1112
The read function may be called multiple times.
13+
14+
Example 1:
15+
16+
Given buf = "abc"
17+
read("abc", 1) // returns "a"
18+
read("abc", 2); // returns "bc"
19+
read("abc", 1); // returns ""
20+
21+
Example 2:
22+
23+
Given buf = "abc"
24+
read("abc", 4) // returns "abc"
25+
read("abc", 1); // returns ""
1226
*/
1327
public class _158 {
1428

15-
/**
16-
* @param buf Destination buffer
17-
* @param n Maximum number of characters to read
18-
* @return The number of characters read
19-
*/
20-
private int buffPtr = 0;
21-
private int buffCnt = 0;
22-
private char[] buff = new char[4];
23-
24-
public int read(char[] buf, int n) {
25-
int ptr = 0;
26-
while (ptr < n) {
27-
if (buffPtr == 0) {
28-
buffCnt = read4(buff);
29-
}
30-
if (buffCnt == 0) {
31-
break;
32-
}
33-
while (ptr < n && buffPtr < buffCnt) {
34-
buf[ptr++] = buff[buffPtr++];
35-
}
36-
if (buffPtr >= buffCnt) {
37-
buffPtr = 0;
38-
}
39-
}
40-
return ptr;
41-
}
42-
43-
//This is a fake method to make IDE happy.
44-
private int read4(char[] buff) {
45-
return 1;
46-
}
47-
29+
public static class Solution1 {
30+
/**
31+
* @param buf Destination buffer
32+
* @param n Maximum number of characters to read
33+
* @return The number of characters read
34+
*/
35+
private int buffPtr = 0;
36+
private int buffCnt = 0;
37+
private char[] buff = new char[4];
38+
39+
public int read(char[] buf, int n) {
40+
int ptr = 0;
41+
while (ptr < n) {
42+
if (buffPtr == 0) {
43+
buffCnt = read4(buff);
44+
}
45+
if (buffCnt == 0) {
46+
break;
47+
}
48+
while (ptr < n && buffPtr < buffCnt) {
49+
buf[ptr++] = buff[buffPtr++];
50+
}
51+
if (buffPtr >= buffCnt) {
52+
buffPtr = 0;
53+
}
54+
}
55+
return ptr;
56+
}
57+
58+
//This is a fake method to make IDE happy.
59+
private int read4(char[] buff) {
60+
return 1;
61+
}
62+
}
4863
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy