十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
119. Pascal's Triangle II
睢縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
代碼如下:(使用雙數(shù)組處理,未優(yōu)化版)
class Solution { public: vectorgetRow(int rowIndex) { vector curVec; vector nextVec; if(rowIndex < 0) return curVec; for(int i = 0;i <= rowIndex; i++) { for(int j = 0;j<=i;j++) { if(j == 0) nextVec.push_back(1); else { if(j >= curVec.size()) nextVec.push_back(curVec[j-1]); else nextVec.push_back(curVec[j] + curVec[j-1]); } } curVec.swap(nextVec); nextVec.clear(); } return curVec; } };
使用思路:
The basic idea is to iteratively update the array from the end to the beginning.
從后到前來更新結(jié)果數(shù)組。
參考自:https://discuss.leetcode.com/topic/2510/here-is-my-brief-o-k-solution
class Solution { public: vectorgetRow(int rowIndex) { vector result(rowIndex+1, 0); result[0] = 1; for(int i=1; i =1; j--) result[j] += result[j-1]; return result; } };
2016-08-12 10:46:10