Given an integer array nums, find the sum of the elements between indices left and right inclusive, where (left <= right).
Implement the NumArray class:
class NumArray:
def __init__(self, nums: List[int]):
self.numArray = nums
def sumRange(self, left: int, right: int) -> int:
result = 0
for i in range(left, right+1):
result += self.numArray[i]
return result
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)
나와있는 그대로~
Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.
"""
The read4 API is already defined for you.
@param buf4, a list of characters
@return an integer
def read4(buf4):
# Below is an example of how the read4 API can be called.
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
buf4 = [' '] * 4 # Create buffer with enough space to store characters
read4(buf4) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
read4(buf4) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
read4(buf4) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
"""
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)
"""
# 이게 뭔 소리야
file = [] # 최종적으로 file 이 됨
cnt = 0 # 첫번째 for 문을 나가기 위한 조건: n 길이까지 읽도록 (file 길이 > n)
out = 0 # 첫번째 for 문을 나가기 위한 조건: file 끝까지 읽도록(file 길이 < n)
# 범위 지정
r = n // 4
# 4로 나눈 나머지가 있으면 한번 더 read4 하도록
if n % 4 != 0:
r += 1
# file 찾기
for i in range(r):
read4(buf)
for j in range(4):
# file 을 다 읽으면 out = 1
if buf[j] == ' ':
out = 1
break
cnt += 1
file.append(buf[j])
buf[j] = ' ' # 본 부분은 ' ' 로 초기화 해주기 => 안해주면 다음 read 때 남아있음
# file 을 다 읽었거나 n 길이만큼 읽었을 경우 break
if out or cnt >= n:
break
# file 을 buf 에 그대로 copy
for i in range(len(file)):
buf[i] = file[i]
# file = "abc", n = 4 처럼 (file 길이 < n) 의 경우 return len(file)
if len(file) < n:
return len(file)
# (file 길이 > n) 의 경우 return n
return n
다시 만나고 싶지 않은 문제네요
file 찾아서 buf 에 다시 copy 해주기~