C++ solution,2ms ,support n step


  • 0
    J
    class Solution {
    public:
    	static const int MAXSTEP=2;
        int climbStairs(int n) {
    		if(n<1){
    			return 0;
    		}
    		deque<int> k(MAXSTEP,0);
    		int tempCount=0;
    		for(int i=0;i<=n;i++){
    			for(int step=1;step<=MAXSTEP;step++)
    			{
    				if(i-step>0){
    					tempCount=tempCount+k[MAXSTEP-step];
    				}
    
    				if(step==i){
    					tempCount++;
    				}
    			}
    			k.pop_front();
    			k.push_back(tempCount);
    			tempCount=0;
    		}
    		return k.back();
        }
    };

Log in to reply
 

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