My python solution with memorization (easy to understand)

  • 0
    def f(self, n):
        if self._result.has_key(n):
            return self._result[n]
        self._result[n-2] = self.f(n-2)
        self._result[n-1] = self.f(n-1)
        return self._result[n-2] + self._result[n-1]
    def climbStairs(self, n):
        self._result = dict()
        self._result[2] = 2
        self._result[1] = 1
        self._result[0] = 0
        return self.f(n)

Log in to reply

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