My C++ code with Pascal's triangle


  • 0
    X

    class Solution {
    public:
    int uniquePaths(int m, int n) {

        if(m==1||n==1)
            return 1;
        
        int rowIndex=m+n-2;
        
        
        vector<int> ans(rowIndex+1,1);
        
        for(int i=1;i<rowIndex/2+1;i++)
        {
            ans[i]=ans[rowIndex-i]=(long)ans[i-1]*(long)(rowIndex-i+1)/i;
        }
        
        
        return ans[n-1];//杨辉三角中的第m+n-2行(行序号从0开始)的第n-1个元素,其值为C(m+n-2,n-1);
        
    }
    

    };


Log in to reply
 

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