C++ using vector (accumulate table)


  • 0
    class Solution {
    public:
    	int climbStairs(int n) {
    		int ans = 0;
    		if(n < 3)
    			ans = n;
    		else
    		{
    			vector<int> num(n, 0);
    			num[0] = 1;
    			num[1] = 2;
    			for(int i = 2; i < n; ++i)
    			{
    				num[i] = num[i - 1] + num[i - 2];
    			}
    			ans = num[n - 1];
    		}
    		return ans;
    	}
    };
    

Log in to reply
 

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