本文主要是介绍【LeetCode刷题】--157.用Read4读取N个字符,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
157.用Read4读取N个字符
/*** The read4 API is defined in the parent class Reader4.* int read4(char[] buf4);*/public class Solution extends Reader4 {/*** @param buf Destination buffer* @param n Number of characters to read* @return The number of actual characters read*/public int read(char[] buf, int n) {//int read4(char[] buf)。根据定义,方法 read4 每次读取 k 个字符(k 不超过 4),并将读取的字符存入其输入参数 buf 中int index = 0;char[] tmp = new char[4];while(index < n){int count = read4(tmp);if(count == 0){break;}for(int i = 0; i < count ;i++){buf[index+i] = tmp[i];}index+=count;}return Math.min(index,n);}
}
这篇关于【LeetCode刷题】--157.用Read4读取N个字符的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!