题目:

代码:
class Solution {
public:
int gcd(int a,int b){
if(a*b==0) return a+b;
else if(a>=b) return gcd(b,a%b);
else return gcd(a,b%a);
}
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1);
ans[0]=1;
int x;
for(int i=1;i<=rowIndex;i++){
x=gcd(rowIndex-i+1,i);
ans[i]=ans[i-1]/(i/x);
ans[i]*=((rowIndex-i+1)/x);
}
return ans;
}
};
本文介绍了一种使用C++实现的帕斯卡三角形算法,通过求最大公约数(GCD)来简化计算过程,提高了算法效率。代码中详细展示了如何生成指定行数的帕斯卡三角形。
927

被折叠的 条评论
为什么被折叠?



