zoukankan      html  css  js  c++  java
  • Read N Characters from Read4

    The API: int read4(char *buf) reads 4 characters at a time from a file.

    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.

    By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

    Note:
    The read function will only be called once for each test case.

     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      * @param buf Destination buffer
     7      * @param n   Maximum number of characters to read
     8      * @return    The number of characters read
     9      */
    10     public int read(char[] buf, int n) {
    11         char[] temp = new char[4];
    12         int count = 0;
    13         while (count < n) {
    14             int currentCount = read4(temp);
    15             if (currentCount == 0) break;
    16             
    17             int index = 0;
    18             while (index < currentCount && count < n)
    19                 buf[count++] = temp[index++];
    20         }
    21         return count;
    22     }
    23 }
  • 相关阅读:
    Python装饰器理解(新手)
    vue项目随笔
    ajax 请求数据传到后台为空字符
    关于document.body.scrollTop 的谷歌,火狐浏览器兼容问题
    Nginx 反向代理解决浏览器跨域问题
    SpringBoot maven build a new demo
    UI收集
    git
    编译
    网络2
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6395071.html
Copyright © 2011-2022 走看看