Help? Getting "bc" instead of "ab" for ["abc", read(2)]


  • 0
    B

    Not sure what's going on... here's my implementation:

    import collections
    
    class Solution(object):
        ring = collections.deque([], maxlen=4)
        def read(self, buf, n):
            i = 0
            while n:
                while self.ring and n:
                    buf[i] = self.ring.popleft()
                    i += 1
                    n -= 1
                if n:
                    temp = ['']*4
                    if not read4(temp):
                        break
                    for c in temp:
                        if not c:
                            continue
                        self.ring.append(c)
            return i
    

    Here's what I used to test and I got the correct answer in another interpreter:

    def read4(temp):
        s = 'abc'
        for i in range(len(s)):
            temp[i] = s[i]
        return len(s)
        
    a = Solution()
    buf = ['']*26
    a.read(buf, 2)
    

Log in to reply
 

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