-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPascalsTriangleII.cs
41 lines (38 loc) · 1.25 KB
/
PascalsTriangleII.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* ==============================================================================
* 功能描述:PascalsTriangleII
* 创 建 者:gz
* 创建日期:2017/4/21 8:24:50
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//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?
namespace Array.Lib
{
/// <summary>
/// 119 : Pascal’s Triangle II
/// </summary>
public class PascalsTriangleII
{
public IList<int> GetRow(int rowIndex)
{
if (rowIndex < 0) return new List<int>();
IList<int> levelList = new List<int>();
for (int i = 0; i <= rowIndex; i++)
{
int k = levelList.Count;
for (int j = k - 1; j >= 1; j--) //这个循环是最重要的一部分,巧妙的运用了Pascal三角的特点
{
levelList[j] = levelList[j - 1] + levelList[j];
}
levelList.Add(1);
}
return levelList;
}
}
}