Problem Solving
157. Read N Characters Given Read4
enumclass
2020. 9. 8. 10:34
Account Login - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
- 문제 내용
4 length의 buffer로 file에서 char를 read해오는 Method "read4"를 사용해서, 목표한 길이의 buffer를 요청한 목표 buffer에 채우고 채워진 buffer의 length를 return 한다.
- 접근 방법
확인 해야할 내용은 다음과 같다.
- 목표 길이보다 Read Buffer가 많을 경우
- 목표 길이보다 Read Buffer가 작을 경우
- 목표 길이와 Read Buffer가 같을 경우
상기 3가지를 고려해서 개발 하면 된다.
var solution = function(read4) {
return function(buf, n) {
let num = 4;
while(buf.length < n && num == 4){
let arr = new Array();
num = read4(arr);
for(let i = 0; i < num; i++){
if(buf.length < n) buf.push(arr[i]);
}
}
return Math.min(n, buf.length);
};
};728x90
반응형