LeetCode-Easy部分中标签为Array#118: Pascal’s Triangle
2017-03-27 22:51
423 查看
原文
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] ]
题目意思
生成指定行数的Pascal’s Triangle。题目分析
总结这个图形的规律,利用数学归纳法推导。设返回的变量类型为如下,实例pastri,IList<IList<int>> pastri; 已知 pastri[0] = new List<int>(); pastri[1] = new List<int>(){1}; 若已知 pastri[i] = new List<int>{1,a1,...,ai-1,ai}; //共i个数 则,pastri[i+1] = new List<int>{1,1+a1,...,ai-1+ai,1}; //共i+1个数
代码实现
public class Solution { public IList<IList<int>> Generate(int numRows) { IList<IList<int>> rtn = new List<IList<int>>(); for(int i=0; i<numRows;i++) { IList<int> ints = new List<int>(); ints.Add(1); for(int j=1;j<i;j++) { IList<int> intsPreRow = rtn[i-1]; ints.Add(intsPreRow[j-1]+intsPreRow[j]); } if(i>0) ints.Add(1); rtn.Add(ints); } return rtn; } }
更多LeetCode题目
LeetCode-题目按tag分类LeetCode-Easy部分中标签为Array的所有题目
相关文章推荐
- LeetCode-Easy部分中标签为Array#119 : Pascal’s Triangle II
- Leetcode 118. Pascal's Triangle (Easy) (cpp)
- LeetCode-Easy部分标签为HashTable#136 Single Number
- LeetCode-Easy部分中标签为Math 507. Perfect Number
- LeetCode-Easy部分中标签为LinkedList 160. Intersection of Two Linked Lists
- LeetCode-Easy部分中标签为Two Pointers 88. Merge Sorted Array
- LeetCode-Easy部分中标签为Array的所有题目
- LeetCode-Easy部分中标签为LinkedList的所有题目
- LeetCode-Easy部分中标签为Two Pointers的所有题目
- LeetCode-Easy部分中标签为Math的所有题目
- LeetCode-Easy部分中标签为LinkedList 203 Remove Linked List Elements
- LeetCode-Easy部分标签为LinkedList 237. Delete Node in a Linked List
- LeetCode-Easy部分标签为HashTable 1. Two Sum(一种战胜95%的提交版本的算法)
- LeetCode-Easy部分中标签为Two Pointers 345. Reverse Vowels of a String
- [Leetcode 118 and 119, Easy] Pascal's Triangle (I and II)
- LeetCode-Easy部分中标签为Array#414 : Third Maximum Number
- LeetCode-Easy部分标签为HashTable 463. Island Perimeter
- LeetCode-Easy部分中标签为HashTable的所有题目
- LeetCode-Easy部分标签为LinkedList 206. Reverse Linked List
- LeetCode-Easy部分中标签为Two Pointers 125. Valid Palindrome