一. 题目描写叙述

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,

Return:

  1. [
  2. [1],
  3. [1,1],
  4. [1,2,1],
  5. [1,3,3,1],
  6. [1,4,6,4,1]
  7. ]

二. 题目分析

关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk4lmbkb1aqbqikBU-ndiMlTF20fq2QUjTTFTeTohZ72KFxgBnz4sJha

将该三角形的左边对齐。就行发现,tri[i][j] = tri[i-1][j-1] + tri[i-1][j]。因此这里的代码并没有什么技巧。

三. 演示样例代码

  1. class Solution {
  2. public:
  3. vector<vector<int>> generate(int numRows) {
  4. vector<vector<int>> result;
  5. if (numRows <= 0)
  6. return result;
  7. else
  8. {
  9. vector<int> temp(1, 1);
  10. result.push_back(temp);
  11. if (numRows == 1) return result;
  12. temp.push_back(1);
  13. result.push_back(temp);
  14. if (numRows == 2) return result;
  15. for (int i = 2; i < numRows; ++i)
  16. {
  17. vector<int> temp(i + 1, 1);
  18. for (int j = 1; j < i; ++j)
  19. temp[j] = result[i - 1][j - 1] + result[i - 1][j];
  20. result.push_back(temp);
  21. }
  22. }
  23. return result;
  24. }
  25. };

leetcode笔记:Pascal&#39;s Triangle的更多相关文章

  1. 【Leetcode】Pascal&#39;s Triangle II

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

  2. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

  3. LeetCode——Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  5. LeetCode——Pascal&#39;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. leetcode - Pascal&#39;s Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  7. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  8. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  9. Pascal&#39;s Triangle II

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

随机推荐

  1. Working out (DP)

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  2. 九度oj 题目1017:还是畅通工程

    题目描述:     某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...

  3. POJ 2092 Grandpa is Famous

    Grandpa is Famous Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7153   Accepted: 3624 ...

  4. RAC+单节点搭建DG

    primary RAC to single standby 参考文献:RAC+单实例DATAGUARD 配置   http://blog.csdn.net/miyatang/article/detai ...

  5. MATLAB(1)

    前言 之前经常用MATLAB,却不小心停留在了舒适区,连基本的调试方法都没有掌握.本文主要是对MATLAB程序调试中的一般方法进行总结,也是自己学习的记录.全文大致分为三个段落: 1)代码内调试: 2 ...

  6. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  7. iOS 设置RGB色的宏

    转自:http://lizhuang.iteye.com/blog/1931768 
//RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...

  8. 1861 奶牛的数字游戏 2006年USACO

    codevs——1861 奶牛的数字游戏 2006年USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Descript ...

  9. python3.x之print()

    1.print内容 #!/usr/bin/python print('hello world')    //print("hello world") 2.print变量 #!/us ...

  10. luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...