forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,再使劲,那个值会越来越大。 | ||
|
||
所以杨辉三角,不要被什么三角迷惑,拉拉手边的皮筋,规律就在里面。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |