File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ My accepted leetcode solutions to some of the common interview problems.
2424- [ Meeting Rooms] ( problems/src/array/MeetingRooms.java ) (Easy)
2525- [ Longest Continuous Increasing Subsequence] ( problems/src/array/LongestIncreasingSubsequence.java ) (Easy)
2626- [ Sparse Matrix Multiplication] ( problems/src/array/SparseMatrixMultiplication.java ) (Medium)
27+ - [ Read N Characters Given Read4] ( problems/src/array/ReadNCharacters.java ) (Easy)
2728
2829#### [ Backtracking] ( problems/src/backtracking )
2930
Original file line number Diff line number Diff line change 1+ package array ;
2+
3+ /**
4+ * Created by gouthamvidyapradhan on 09/12/2017.
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
8+ left in the file.
9+
10+ By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
11+
12+ Note:
13+ The read function will only be called once for each test case.
14+
15+
16+ */
17+ public class ReadNCharacters {
18+
19+ /**
20+ * Main method
21+ * @param args
22+ */
23+ public static void main (String [] args ) {
24+
25+ }
26+
27+ /**
28+ *
29+ * @param buf
30+ * @param n
31+ * @return
32+ */
33+ public int read (char [] buf , int n ) {
34+ int i = 0 ;
35+ int toRead = Math .min (n , buf .length );
36+ while (i < toRead ){
37+ char [] temp = new char [4 ];
38+ int r = read4 (temp );
39+ for (int j = 0 ; j < r && i < toRead ; j ++){
40+ buf [i ] = temp [j ];
41+ i ++;
42+ }
43+ if (r < 4 ) break ;
44+ }
45+ return Math .min (i , toRead );
46+ }
47+
48+
49+ private int read4 (char [] buf ){
50+ return 1 ;//return fake value just to resolve compilation error
51+ }
52+
53+ }
You can’t perform that action at this time.
0 commit comments