LeetCode:Pascal's Triangle

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

分析:简单的模拟从第一层开始计算即可

 class Solution {
public:
vector<vector<int> > generate(int numRows) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> >res;
if(numRows == )return res;
res.push_back(vector<int>{});
if(numRows == )return res;
vector<int>tmp;
tmp.reserve(numRows);
for(int i = ; i <= numRows; i++)
{
tmp.clear();
tmp.push_back();
for(int j = ; j < i-; j++)
tmp.push_back(res[i-][j-]+res[i-][j]);
tmp.push_back();
res.push_back(tmp);
}
return res;
}
};

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,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

分析:每次计算只和上一层有关系,因此只需要一个数组空间就可以                                                                                                            本文地址

 class Solution {
public:
vector<int> getRow(int rowIndex) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<int> res(rowIndex+,);
if(rowIndex == )return res;
for(int i = ; i <= rowIndex; i++)
{
int tmp = res[];
for(int j = ; j <= i-; j++)
{
int kk = res[j];
res[j] = tmp+res[j];
tmp = kk;
}
}
return res;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3436562.html

LeetCode:Pascal's Triangle I II的更多相关文章

  1. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  2. [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, ...

  3. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  4. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  5. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  6. LeetCode Pascal's Triangle && Pascal's Triangle II Python

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

  7. LeetCode - Pascal's Triangle II

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

  8. 【leetcode】Pascal's Triangle I & II (middle)

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

  9. LeetCode——Pascal's Triangle II

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

随机推荐

  1. centos 安装完Nginx后,为什么访问不了?

    很多时候,安装完成后,服务也启动了 但是访问不了 看看是不是防火墙开启了: 本地试下端口是否可访问telnet 192.168.4.155 80 CentOS 7默认使用的是firewall作为防火墙 ...

  2. qsort

    /*** *qsort.c - quicksort algorithm; qsort() library function for sorting arrays * Copyright (c) Mic ...

  3. Effective Java 10 Always override toString() method

    Advantage Provide meaningful of an object info to client. Disadvantage Constrain the ability of chan ...

  4. java运算符总结

    1.算数运算符:+.-.*./.%(加减乘除取余) 2.自增自减:++.-- 3.赋值运算符:=.+=.-=.*=./= 4.关系运算符:>.<.>=.<=.==.!= 逻辑运 ...

  5. 在VS项目中使用SVN版本号作为编译版本号

    在实际项目中(特别是作为产品的项目),版本号是必不可少的一部分.版本号的规则也有许多种,在此不讨论具体的编码规范.对于迭代的产品,版本繁多,特别是有多个实施项目所使用产品的版本不同(基于定制需求)时, ...

  6. Tomcat常用的优化技巧

    (1)屏蔽DNS查询 Web应用程序可以通过Web容器提供的getRemoteHost()方法获得访问Web应用客户的IP地址和名称,但是这样会消耗Web容器的资源,并且还需要通过IP地址和DNS服务 ...

  7. 微信公众平台开发教程--方培工作室,PHP语言版本

    准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消 ...

  8. LOJ Finding LCM(math)

    1215 - Finding LCM Time Limit: 2 second(s) Memory Limit: 32 MB LCM is an abbreviation used for Least ...

  9. EM basics- the Maxwell Equations

    All the two important problems that the EM theory trys to describe and explain are propogation and r ...

  10. Java语法结构

    一.顺序结构(从上往下依次执行) 顺序结构语法比较简单,从上往下依次执行即可. 二.选择结构(选择性执行,如果....则.....) 1.if 语句 if语句,作用是根据判断结果为真或假,选择其中一个 ...