0

not sure about the problem, so might misleading in the title. this question from LC 77. below is my code, but it returns

TypeError: object of type 'NoneType' has no len()

however, if I change path.append(lis[i]) to path + [[lis[i]], the code works well and passes the test. what's the difference between these two? Thank you!

def combine( n: int, k: int) -> List[List[int]]:      
        res = []
        lis = [i for i in range(1,n+1)]
        visited = set()
        def dfs(lis, path):
            if len(path) == k:    
                res.append(path)
                return
            for i in range(len(lis)):
                dfs(lis[i+1:], path.append(lis[i]))
        dfs(lis, [])
        return res
petezurich
  • 9,280
  • 9
  • 43
  • 57
fyxc
  • 1
  • 2

1 Answers1

1

path.append() returns None and modifies path in-place. You probably meant to call path.append(lis[i]) before recursively calling dfs(lis[i+1:], path)

rchome
  • 2,623
  • 8
  • 21