[LeetCode] Read N Characters Given Read4 I & II

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.

 1 // Forward declaration of the read4 API.
 2 int read4(char *buf);
 3
 4 class Solution {
 5 public:
 6     /**
 7      * @param buf Destination buffer
 8      * @param n   Maximum number of characters to read
 9      * @return    The number of characters read
10      */
11     int read(char *buf, int n) {
12         char tmp[4];
13         int idx = 0, cnt4;
14         while (idx < n) {
15             cnt4 = read4(tmp);
16             for (int i = 0; i < cnt4 && idx < n; ++i) {
17                 buf[idx++] = tmp[i];
18             }
19             if (cnt4 < 4) break;
20         }
21         return idx;
22     }
23 };

Read N Characters Given Read4 II - Call multiple times

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 may be called multiple times.

 1 // Forward declaration of the read4 API.
 2 int read4(char *buf);
 3
 4 class Solution {
 5 private:
 6     char tmp[4];
 7     int tmp_idx = 0, tmp_len = 0;
 8 public:
 9     /**
10      * @param buf Destination buffer
11      * @param n   Maximum number of characters to read
12      * @return    The number of characters read
13      */
14     int read(char *buf, int n) {
15         int idx = 0;
16         bool flag;
17         while (idx < n) {
18             flag = true;
19             if (tmp_idx == tmp_len) {
20                 tmp_idx = 0;
21                 tmp_len = read4(tmp);
22                 if (tmp_len != 4) flag = false;
23             }
24             for (; tmp_idx < tmp_len && idx < n; ++tmp_idx) {
25                 buf[idx++] = tmp[tmp_idx];
26             }
27             if (!flag) break;
28         }
29         return idx;
30     }
31 };
时间: 2024-07-30 11:28:39

[LeetCode] Read N Characters Given Read4 I & II的相关文章

[leetcode]Read N Characters Given Read4

用read4实现readn... 至调用一次,感觉怎么搞都可以...估计这个题有II就是调用多次了... 感觉多次勇哥buffer存下多读的那部分就好了... // Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** * @param buf Destination buffer * @param n Maximum number of characters to rea

[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times

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

[Locked] Read N Characters Given Read4 &amp; Read N Characters Given Read4 II - Call multiple times

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 rea

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise re

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

leetcode -day28 Unique Binary Search Trees I II

1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 /

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave