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]
]

解题思路1:迭代插入(未通过)

[              [               [                      [
[1], [1], [1], [1],
[1], [1,1], [1,1], [1,1]
[1], ==> [1,2], ==> [1,2,1], ==>...==> [1,2,1],
[1], [1,3], [1,3,3], [1,3,3,1],
[1] [1,4] [1,4,6], [1,4,6,4,1],
] [ [ [

可以推导出每一列的递推公式:

第一列从第一行开始:1

第二列从第二行开始:j / 1,  j∈[1,n)

第三列从第三行开始:[j*(j-1)] / [1*2], j∈[2,n)

...

第M列从第M行开始:[j*(j-1)*...*(j-m+2)] / [1*2*...*m-1], j∈[m,n)

但是程序未能实现,在输入numRows=8时,出现Runtime Error错误。

【★】解题思路2:

用f(n)(m)表示第n行第m个元素,n从1开始,1≤m≤n;

则f()(1)=f()(n)=1, f(n)(m)=f(n-1)(m-1) + f(n-1)(m);

核心代码只有四步:

①新建符合的空间;

②将首尾置为1;

③遍历除首尾外的元素空间,利用公式填充;

④将填充好的列表放入结果队列里;

 class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > rowslst; if (numRows == )
return rowslst; for (int i=; i<numRows; ++i) {
vector<int> row(i+);
row[] = row[i] = ; for(int j=; j<i; ++j) {
row[j] = rowslst[i-][j-] + rowslst[i-][j];
} rowslst.push_back(row);
} return rowslst;
}
};

附录:

杨辉三角与计算机

【Leetcode】【Easy】Pascal's Triangle的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. 【leetcode刷题笔记】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

  8. LeetCode Array Easy 118. Pascal's Triangle

    Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. HDU2588

    结论:如果p是n的约数,那么满足gcd(i,n)==p的i的个数是Φ(n/p) #include<bits/stdc++.h> using namespace std; const int ...

  2. Linux acpi off学习的必要

    ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...

  3. 5.centos7 jenkins安装

    1.安装jdk 安装过程请参照,zookeeper 安装中的jdk安装章节 文章地址: 2.安装jenkins 添加Jenkins库到yum库,Jenkins将从这里下载安装. wget -O /et ...

  4. apply、call、bind区别、用法

    apply和call都是为了改变某个函数运行时的上下文而存在的(就是为了改变函数内部this的指向):   如果使用apply或call方法,那么this指向他们的第一个参数,apply的第二个参数是 ...

  5. 一个优秀的app应该考虑的问题

    带着团队做了3个app,需求是客户决定的,甚至连进度都不是项目经理可以控制的(譬如说一个app要在6周内,3个人完成).现在的状态是基本上没有用户量,当然原因是多方面的,下面说一说我认为app设计的原 ...

  6. 个人笔记——Android网络技术

    一.WebView 的用法 Android 提供WebView 的用法,可以在自己的应用程序里嵌入一个浏览器 webView.getSettings().setJavaScriptEnabled(tr ...

  7. storm中几个概念的大小关系

    从图可以看出来:topology>supervisor>worker>excutor>task; 也就是说一个topology可以运行在多个supervisor上,一个supe ...

  8. java多线程lock的使用

    看代码: package com.ming.thread.reentrantlock; import java.util.concurrent.locks.Lock; import java.util ...

  9. Where Should an Architect Begin?--reference

    http://www.bitnative.com/2014/01/24/where-should-a-software-architect-begin/ Where Should an Archite ...

  10. angular 首屏优化

    前一段时间把公司的一个angular项目做了一次大的优化,记录一下过程. 起因: 起因是用户反映网站加载时间过长,从loading画面显示到页面可响应要13s,对于一般的页面恐怕没有用户愿意等待这么久 ...