Leetcode 119. Pascal's Triangle II (Easy) (cpp)

来源:互联网 发布:古天乐 希望小学 知乎 编辑:程序博客网 时间:2024/05/21 06:01

Leetcode 119. Pascal's Triangle II (Easy) (cpp)

Tag: Array

Difficulty: Easy


/*119. Pascal's Triangle II (Easy)Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].*/class Solution {  public:  vector<int> getRow(int rowIndex) {  vector<int> res(1, 1);  if (rowIndex == 0) {            return res;          }for (int i = 1; i <= rowIndex; i++) {  vector<int> tmp = res;  res.clear();  res.push_back(1);  for (int i = 0; i < tmp.size() - 1; i++) {res.push_back(tmp[i] + tmp[i + 1]);              }res.push_back(1);  }  return res;  }  };


0 0