Skip to content

Commit

Permalink
added 50. Pascal's Triangle II
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 3, 2014
1 parent 0b1ff3f commit 6770fe8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 50. Pascal's Triangle II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
基础题,不多说,思路和 [32. Pascal's Triangle](32. Pascal's Triangle) 完全一致。

-----

重点总结一下杨辉三角的本质,我们提到杨辉三角,不要老想起那个正三角形的样子,那个样子只是让你更加形象的感受杨辉三角透露出来的数学美感,但在计算机的世界里,那东西就是格式拼凑的结果而已。

我们应该想起的本质,其实应该是**一根皮筋**。如下图所示:

```cpp
|1| ------> drag

|1|2|1| ------> drag

|1|3|3|1| ------> drag

|1|4|6|4|1| ------> drag
```

这样看起来,有没有点皮筋的感觉?你拽它,越拽越长,而中间受到的力,越来越大,使劲拽,会有一种要崩的感觉。上面图里,我最大拽到了6,再使劲,那个值会越来越大。

所以杨辉三角,不要被什么三角迷惑,拉拉手边的皮筋,规律就在里面。
11 changes: 11 additions & 0 deletions 50. Pascal's Triangle II/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "solution.h"
#include <iostream>

int main()
{
Solution s;
for (auto i : s.getRow(4))
std::cout << i << " ";

return 0;
}
14 changes: 14 additions & 0 deletions 50. Pascal's Triangle II/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <vector>
using std::vector;

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> vec(rowIndex+1, 0);
vec[0] = 1;
for (int i=0; i<=rowIndex; ++i)
for (int j=i; j>0; --j)
vec[j] += vec[j-1];
return vec;
}
};

0 comments on commit 6770fe8

Please sign in to comment.