题目

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

For example, given numRows = 5,

Return

分析

构建数字金字塔,由上图可以清楚的找到规律。

该题目可用递归实现!

比较简单~

AC代码

class Solution {
public:
vector<vector<int>> generate(int numRows) {
if (numRows == 0)
return vector<vector<int>>();
else if (numRows == 1)
return vector<vector<int>>(1, vector<int>(1, 1)); //存储当前金字塔
vector<vector<int> > ret = generate(numRows - 1);
//计算当前行
vector<int> cur(numRows, 0);
cur[0] = 1;
cur[numRows - 1] = 1; for (int i = 1; i < numRows - 1; ++i)
{
cur[i] = ret[numRows - 2][i - 1] + ret[numRows - 2][i];
}//for
ret.push_back(cur); return ret;
}
};

GitHub测试程序源码

LeetCode(118) Pascal's Triangle的更多相关文章

  1. LeetCode(119) Pascal's Triangle II

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

  2. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode(118):杨辉三角

    Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. 2017 Multi-University Training Contest - Team 1 Add More Zero

    Problem Description There is a youngster known for amateur propositions concerning several mathemati ...

  2. bzoj 5449 序列

    https://www.lydsy.com/JudgeOnline/problem.php?id=5449 话说很早以前做过..算是IDA*的板子吧,一个简单的估价函数就可以过去了 %:pragma ...

  3. morphia(3)-查询

    1.查询所有 @Test public void query() throws Exception { final Query<Employee> query = datastore.cr ...

  4. Linux之文本处理命令

    Sort 将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. -u     在输出行中去除重复行 -r      改为降序(默认升序) ...

  5. Oracle Database Hang While Loading 3rd party SBT Library And After This Nobody Can Access The Database (windows login 登陆hang )

    Applies to: Oracle Database - Enterprise Edition - Version 11.2.0.4 and later Microsoft Windows x64 ...

  6. 095 Unique Binary Search Trees II 不同的二叉查找树 II

    给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树:   1         3     3      2      1    ...

  7. javascript要点(上)

    立即执行函数 即Immediately Invoked Function Expression (IIFE),正如它的名字,就是创建函数的同时立即执行.它没有绑定任何事件,也无需等待任何异步操作: ( ...

  8. java中的线程安全是什么?什么叫线程安全?什么叫不安全?

    java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...

  9. javascript for/forEach

    基本用法 for:for(var i=0;i<arr.length;i++) forEach:arr.forEach(function(value,index,arr){},),其中functi ...

  10. setuid

    -r-s--x--x   #s就是setuid,仅可用在二进制文件,对目录设置无效