-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
157 Read N Characters Given Read4.swift
48 lines (39 loc) 路 1.22 KB
/
157 Read N Characters Given Read4.swift
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
//
// 157 Read N Characters Given Read4.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 27/08/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/read-n-characters-given-read4/
class Solution: Reader4 {
/// Reads n characters.
///
/// - Parameters:
/// - buf: Destination buffer.
/// - n: The number of characters to read.
/// - Returns: The number of actual characters reads.
///
/// - Complexity:
/// - time: O(n), where n is the number of characters to read.
/// - space: O(1), only constant space is used.
func read(_ buf: inout [Character], _ n: Int) -> Int {
var copiedChars = 0
var readChars = 4
var tmpBuf = [Character](repeating: " ", count: 4)
while copiedChars < n, readChars == 4 {
readChars = read4(&tmpBuf)
for i in 0..<readChars {
guard copiedChars != n else { return copiedChars }
buf[copiedChars] = tmpBuf[i]
copiedChars += 1
}
}
return copiedChars
}
}
/// Provided Code
class Reader4 {
func read4(_ buf4: inout [Character]) -> Int
}