# The read4 API is already defined for you.# def read4(buf4: List[str]) -> int:classSolution:def__init__(self):self.buf4=[None]*4self.i=self.size=0defread(self,buf:List[str],n:int)->int:j=0whilej<n:ifself.i==self.size:self.size=read4(self.buf4)self.i=0ifself.size==0:breakwhilej<nandself.i<self.size:buf[j]=self.buf4[self.i]self.i+=1j+=1returnj
/** * The read4 API is defined in the parent class Reader4. * int read4(char[] buf4); */publicclassSolutionextendsReader4{privatechar[]buf4=newchar[4];privateinti;privateintsize;/** * @param buf Destination buffer * @param n Number of characters to read * @return The number of actual characters read */publicintread(char[]buf,intn){intj=0;while(j<n){if(i==size){size=read4(buf4);i=0;if(size==0){break;}}while(j<n&&i<size){buf[j++]=buf4[i++];}}returnj;}}
/** * The read4 API is defined in the parent class Reader4. * int read4(char *buf4); */classSolution{public:/** * @param buf Destination buffer * @param n Number of characters to read * @return The number of actual characters read */intread(char*buf,intn){intj=0;while(j<n){if(i==size){size=read4(buf4);i=0;if(size==0)break;}while(j<n&&i<size)buf[j++]=buf4[i++];}returnj;}private:char*buf4=newchar[4];inti=0;intsize=0;};
/** * The read4 API is already defined for you. * * read4 := func(buf4 []byte) int * * // Below is an example of how the read4 API can be called. * file := File("abcdefghijk") // File is "abcdefghijk", initially file pointer (fp) points to 'a' * buf4 := make([]byte, 4) // Create buffer with enough space to store characters * read4(buf4) // read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e' * read4(buf4) // read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i' * read4(buf4) // read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file */varsolution=func(read4func([]byte)int)func([]byte,int)int{buf4:=make([]byte,4)i,size:=0,0// implement read below.returnfunc(buf[]byte,nint)int{j:=0forj<n{ifi==size{size=read4(buf4)i=0ifsize==0{break}}forj<n&&i<size{buf[j]=buf4[i]i,j=i+1,j+1}}returnj}}