Leetcode 157:read N characters Given Read4

Source: Internet
Author: User

Read N characters Given Read4Total Accepted: 960 Total Submissions: 3775

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 are only 3 characters left in the file.

read4by using the API, implement the function that int read(char *buf, int n) readsn characters from the file.

Note:
The function is only being read called once for each test case.

Analysis

None

Note

None

[CODE]

/* The READ4 API is defined in the parent class Reader4.      int Read4 (char[] buf);  */public class Solution extends Reader4 {    /**     * @param buf Destination Buffer     * @param n   Maximum number of Characters to read     * @return    The number of characters read     *    /public int read (char[] buf, int n) {        if (Buf==null | | n<=0) return 0;        int i=0;        char[] temp = new Char[4];        while (n>0) {            int x = READ4 (temp);            int j = 0;            int k = x;            while (n>0 && x>0) {                buf[i++] = temp[j++];                --x;                --n;            }            if (k<4) break;        }        return i;    } }


Leetcode 157:read N characters Given Read4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.