9-line Python solution


  • 2
     class Solution(object):
        def __init__(self):
            self.temp, self.start, self.num = [""] * 4, 0, -1
     
        def read(self, buf, n, idx=0):
            while n > 0:
                while self.start < self.num and n:
                    buf[idx] = self.temp[self.start]
                    idx, self.start, n = idx + 1, self.start + 1, n - 1
                if not n: break
                self.num, self.start = read4(self.temp), 0
                if not self.num: break
            return idx
        
        # 1-line init(), 8-line read()
        # Runtime: 54ms

  • 0

    Translated mine to Python, using all of your dirty tricks :-P

    class Solution(object):
        def __init__(self):
            self.buf4, self.i4, self.n4 = [None] * 4, 0, 0
    
        def read(self, buf, n, i=0):
            while i < n:
                if self.i4 == self.n4:
                    self.i4, self.n4 = 0, read4(self.buf4)
                    if not self.n4: break
                buf[i], i, self.i4 = self.buf4[self.i4], i + 1, self.i4 + 1
            return i
    

    Got another idea I'm trying now...


  • 0

    Ok I implemented that other idea now. I think you'll like it.


  • 0

    I knew you would have liked the optional idx=0 param... And the tuple to increment variables...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.