티스토리 뷰
[LeetCode] 157. Read N Characters Given Read4
Daniel803 2023. 2. 10. 13:28문제 설명은 아래와 같다.
: Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.
Method read4:
The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4.
The return value is the number of actual characters read.
Note that read4() has its own file pointer, much like FILE *fp in C.
Definition of read4:
Parameter: char[] buf4
Returns: int
buf4[] is a destination, not a source. The results from read4 will be copied to buf4[].
Below is a high-level example of how read4 works:
File file("abcde"); // File is "abcde", initially file pointer (fp) points to 'a'
char[] buf4 = new char[4]; // Create buffer with enough space to store characters
read4(buf4); // read4 returns 4. Now buf4 = "abcd", fp points to 'e'
read4(buf4); // read4 returns 1. Now buf4 = "e", fp points to end of file
read4(buf4); // read4 returns 0. Now buf4 = "", fp points to end of file
Method read:
By using the read4 method, implement the method read that reads n characters from file and store it in the buffer array buf. Consider that you cannot manipulate file directly.
The return value is the number of actual characters read.
Definition of read:
Parameters: char[] buf, int n
Returns: int
buf[] is a destination, not a source. You will need to write the results to buf[].
Note:
- Consider that you cannot manipulate the file directly. The file is only accessible for read4 but not for read.
- The read function will only be called once for each test case.
- You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters.
예시는 아래와 같다.
Example 1:
Input: file = "abc", n = 4
Output: 3
Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3.
Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to.
Example 2:
Input: file = "abcde", n = 5
Output: 5
Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5.
Example 3:
Input: file = "abcdABCD1234", n = 12
Output: 12
Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12.
아래는 내가 작성한 솔루션이다. 리트코드에 접속해 문제를 보면 알겠지만, 오늘 기준 문제 추천이 530, 비추천이 3300으로 문제 설명에 대한 불만이 매우 많고, 나 역시 명확하게 다가오지 않았다. 4 bytes, 즉 4글자씩 파일을 읽어오는 read4() 함수를 사용해야하기에 4개의 요소를 갖는 buf4라는 버퍼를 선언했고, 4 bytes씩 읽어오기 시작했다. 읽은 글자의 개수를 반환(Return)하기에 읽은 글자의 수가 4개가 되지 못하면 buf의 마지막을 읽은 것이기에 while문을 통해 끝까지 읽으면서 4개를 읽어오는대로 buf라는 저장해야하는 버퍼에 한 글자씩 저장하는 방식으로 풀이했다.
class Solution:
def read(self, buf, n):
"""
:type buf: Destination buffer (List[str])
:type n: Number of characters to read (int)
:rtype: The number of actual characters read (int)
"""
four = 4
cnt = 0
buf4 = ["" for i in range(4)]
while four == 4:
four = read4(buf4)
for i in range(four):
buf[cnt] = buf4[i]
cnt += 1
return min(n, cnt)
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
[LeetCode] 266. Palindrome Permutation (0) | 2023.02.13 |
---|---|
[LeetCode] 158. Read N Characters Given read4 II - Call Multiple Times (0) | 2023.02.12 |
[LeetCode] 265. Paint House II (0) | 2023.02.08 |
[LeetCode] 359. Logger Rate Limiter (0) | 2022.09.27 |
[LeetCode] 13. 3Sum (0) | 2022.09.12 |
- Total
- Today
- Yesterday
- 오블완
- socket programming
- Computer Graphics
- 머신 러닝
- 소켓 프로그래밍
- Android
- 파이썬
- C++
- 티스토리챌린지
- java
- 투 포인터
- 딕셔너리
- 안드로이드
- Hash Map
- 이코노미스트 에스프레소
- Python
- The Economist
- 이코노미스트
- tf-idf
- DICTIONARY
- ml
- min heap
- I2C
- machine learning
- leetcode
- The Economist Espresso
- join
- vertex shader
- defaultdict
- 리트코드
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |