File tree 3 files changed +38
-1
lines changed
_includes/_root/read-n-characters-given-read4-ii-call-multiple-times
read-n-characters-given-read4-ii-call-multiple-times 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change
1
+ /* The read4 API is defined in the parent class Reader4.
2
+ int read4(char[] buf); */
3
+
4
+ public class Solution extends Reader4 {
5
+
6
+ LinkedList <Character > queue = new LinkedList <Character >();
7
+
8
+ /**
9
+ * @param buf Destination buffer
10
+ * @param n Maximum number of characters to read
11
+ * @return The number of characters read
12
+ */
13
+ public int read (char [] buf , int n ) {
14
+
15
+ char [] _buf = new char [4 ];
16
+
17
+ int total = 0 ;
18
+
19
+ while (true ){
20
+ int l = read4 (_buf );
21
+
22
+ for (int i = 0 ; i < l ; i ++){
23
+ queue .add (_buf [i ]);
24
+ }
25
+
26
+ l = Math .min (n - total , queue .size ());
27
+
28
+ for (int i = 0 ; i < l ; i ++){
29
+ buf [total ++] = queue .poll ();
30
+ }
31
+
32
+ if (l == 0 ) break ;
33
+ }
34
+
35
+ return total ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change 1
1
---
2
2
layout : solution
3
3
title : Read N Characters Given Read4 II - Call multiple times
4
- date : 2014-11-24 17:30:24+08:00
4
+ date : 2014-11-24 17:31:29 +0800
5
5
---
6
6
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
7
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
You can’t perform that action at this time.
0 commit comments