Shared my c++ DP solution


  • 0
    A
    class Solution {
    public:
        int climbStairs(int n) {
        	int k;
        	auto iter = map.find(n);
        	if(iter != map.end())
    		{
    			k = iter->second;
    		}
    		else
    		{
            	if(n <= 0)
                	k = 0;
                else
            		k = n < 3? n : climbStairs(n - 1) + climbStairs(n - 2);
            	map.insert(make_pair(n, k));
    		}
            return k;
        }
    private:
    	std::map<int, int> map;
    };

Log in to reply
 

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