-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0157-ReadNCharactersGivenRead4.cs
63 lines (55 loc) · 1.56 KB
/
0157-ReadNCharactersGivenRead4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//-----------------------------------------------------------------------------
// Runtime: 80ms
// Memory Usage: 22.7 MB
// Link: https://leetcode.com/submissions/detail/389654223/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* The Read4 API is defined in the parent class Reader4.
* int Read4(char[] buf);
*/
public class _0157_ReadNCharactersGivenRead4
{
private readonly string str;
private int index;
public _0157_ReadNCharactersGivenRead4(string str)
{
index = 0;
this.str = str;
}
/**
* @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)
{
if (n == 0) return 0;
char[] myBuf = new char[4];
int idx = 0;
while (idx < n)
{
int curr = Read4(myBuf);
for (int i = 0; i < curr && idx < n; i++)
{
buf[idx] = myBuf[i];
idx++;
}
if (curr < 4) break;
}
return idx;
}
public int Read4(char[] buf)
{
for (int i = 0; i < 4; i++)
{
if (index < str.Length)
buf[i] = str[index++];
else
return i;
}
return 4;
}
}
}