Pascal's triangle

来源:互联网 发布:mac的itunes在哪里 编辑:程序博客网 时间:2024/05/09 17:00

题目:

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1,4,6,4,1]]

思路:

其实很简单,给定一个数,产生杨辉三角。

我们同样用到了 两个 vector来交替得到第i行的数值。解法如同之前的一篇博文Triangle :

class Solution {public:    vector<vector<int> > generate(int numRows) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int> > result;        if(numRows<1)            return result;        vector<int> myvector, tmpvector;        myvector.push_back(1);        result.push_back(myvector);        for(int i=1;i<numRows;i++)        {             tmpvector.push_back(1);            for(int j=1;j<i;j++)                tmpvector.push_back(myvector[j-1]+myvector[j]);            tmpvector.push_back(1);            myvector = tmpvector;            result.push_back(myvector);            tmpvector.clear();        }                       return result;    }};


Run Status: Accepted!
Program Runtime: 12 milli secs

 

Pascal's Triangle II

题目:

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?

解法:这里类似上述代码,就不多说了。

class Solution {public:    vector<int> getRow(int rowIndex) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<int> myvector, tmpvector;         if(rowIndex<0)              return myvector;          myvector.push_back(1);          for(int i=1;i<=rowIndex;i++)          {               tmpvector.push_back(1);              for(int j=1;j<i;j++)                  tmpvector.push_back(myvector[j-1]+myvector[j]);              tmpvector.push_back(1);              myvector = tmpvector;              tmpvector.clear();          }                         return myvector;      }};


 

Run Status: Accepted!
Program Runtime: 16 milli secs

 

 用一个 vector 来存储,得到较好的解法。

class Solution {  public:      vector<int> getRow(int rowIndex) {          // Start typing your C/C++ solution below           // DO NOT write int main() function           vector<int> myvector(rowIndex+1);           if(rowIndex<0)                return myvector;            myvector[0] = 1;            for(int i=1;i<=rowIndex;i++)            {                 myvector[i] = 1;                for(int j=i-1;j>=1;j--)                    myvector[j] = myvector[j-1]+myvector[j];                myvector[0] = 1;            }                           return myvector;        }  };  

 

Run Status: Accepted!
Program Runtime: 16 milli secs


java 语言

public class Solution {    /*         1        1 1       1 2 1      1 3 3 1     1 4 6 4 1     */     public List<Integer> getRow(int rowIndex) {         List<Integer> result = new ArrayList<Integer>(rowIndex + 1);        if (rowIndex < 0) {            return result;        }        result.add(1);        if (rowIndex == 0) {            return result;        }        result.add(1);        if (rowIndex == 1) {            return result;        }        for (int i=2; i<=rowIndex; i++) {            result.add(1);              for (int j = i - 1; j > 0; --j) {                  result.set(j, result.get(j) + result.get(j - 1));              }        }        return result;    }}


原创粉丝点击