zoukankan      html  css  js  c++  java
  • Read N Characters Given 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.

    这题用C 写应该简单很多。

     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         int result = 0;
    12         char[] tmp = new char[4];
    13         while(result < n){
    14             int size = super.read4(tmp);
    15             for(int i = 0; i < size && result < n; i ++, result ++){
    16                 buf[result] = tmp[i];
    17             }
    18             if(size < 4) break;
    19         }
    20         return result;
    21     }
    22 }
  • 相关阅读:
    DockerFile构建步骤及命令
    linux安装nginx及常用命令
    docker常用命令
    Docker安装
    获取TrustedInstaller权限
    获取本机公网ip的url地址
    centOS7配置ip
    VS Code配置c语言环境
    Linux l 2.4.20-8 # 溢出
    VMware Destination Host Unreachable
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4233751.html
Copyright © 2011-2022 走看看