Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b74d7c6

Browse files
refactor 158
1 parent 784217b commit b74d7c6

File tree

1 file changed

+50
-53
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

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

33
/**
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.
6-
7-
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.
8-
9-
By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
10-
11-
Note:
12-
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 ""
4+
* 158. Read N Characters Given Read4 II - Call multiple times
5+
*
6+
* The API: int read4(char *buf) reads 4 characters at a time from a file.
7+
* 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.
8+
* By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
9+
*
10+
* Note:
11+
* The read function may be called multiple times.
12+
*
13+
* Example 1:
14+
* Given buf = "abc"
15+
* read("abc", 1) // returns "a"
16+
* read("abc", 2); // returns "bc"
17+
* read("abc", 1); // returns ""
18+
*
19+
* Example 2:
20+
* Given buf = "abc"
21+
* read("abc", 4) // returns "abc"
22+
* read("abc", 1); // returns ""
2623
*/
2724
public class _158 {
2825

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++];
26+
public static class Solution1 {
27+
/**
28+
* @param buf Destination buffer
29+
* @param n Maximum number of characters to read
30+
* @return The number of characters read
31+
*/
32+
private int buffPtr = 0;
33+
private int buffCnt = 0;
34+
private char[] buff = new char[4];
35+
36+
public int read(char[] buf, int n) {
37+
int ptr = 0;
38+
while (ptr < n) {
39+
if (buffPtr == 0) {
40+
buffCnt = read4(buff);
41+
}
42+
if (buffCnt == 0) {
43+
break;
44+
}
45+
while (ptr < n && buffPtr < buffCnt) {
46+
buf[ptr++] = buff[buffPtr++];
47+
}
48+
if (buffPtr >= buffCnt) {
49+
buffPtr = 0;
50+
}
51+
}
52+
return ptr;
5053
}
51-
if (buffPtr >= buffCnt) {
52-
buffPtr = 0;
53-
}
54-
}
55-
return ptr;
56-
}
5754

58-
//This is a fake method to make IDE happy.
59-
private int read4(char[] buff) {
60-
return 1;
55+
//This is a fake method to make IDE happy.
56+
private int read4(char[] buff) {
57+
return 1;
58+
}
6159
}
62-
}
6360
}

0 commit comments

Comments
 (0)