leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)
题目描述:
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
要完成的函数:
vector<vector<int>> generate(int numRows)
说明:
1、这道题目给定一个行数,要求返回具有给定行数的帕斯卡三角形,结果存储在二维vector中。
2、明白题意,这道题不难,每一行第 j 个元素的数值都是上一行第 j 个元素的数值+上一行第 j -1个元素的数值,最后再push_back一个1。
代码如下(附详解):
vector<vector<int>> generate(int numRows)
{
vector<int>res1;//每一行的vector
vector<vector<int>>res;//存储最后结果的vector
int i;
if(numRows==0)//边界条件,返回一个空的二维vector
return res;
res1.push_back(1);//第一行的vector
while(numRows--)
{
res.push_back(res1);//把当前行的res1压入res中
i=res1.size()-1;
while(i>0)//从res1的后面开始处理,这样不会影响每一步的处理
{
res1[i]=res1[i-1]+res1[i];
i--;
}
res1.push_back(1);//最后再压入一个1
}
return res;
}
上述代码之所以要从res1的后面开始处理,是因为这样不会影响后续的计算处理。
比如res1=[1,2,1],按照上述做法,res1[2]先变成1+2=3,所以res1是[1,2,3],接着再res1[1]=2+1=3,所以res1是[1,3,3],最后再压入一个1,变成最后的[1,3,3,1]。
但如果我们从前面开始处理,res1[1]=1+2=3,所以res1是[1,3,1],接着res1[2]=3+1=4?这时候已经改变了res1中我们需要的原始数值,而从后面开始处理就不会。
上述代码实测3ms,beats 96.52% of cpp submissions。
leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)的更多相关文章
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
随机推荐
- linux fuser的使用
当进行共享存储的时候,umount可能无法用于卸载某个设备,说是被某个进程所占用,但是又无法找到该进程.这个时候使用fuser -km /data命令杀死所有在使用这个存储设备的进程然后再umount ...
- 下拉菜单--JavaScript触发方法
1. $(function(){ $(".dropdown-toggle").one("click",function(){ $(this).dropdown( ...
- [GO]方法集
指针变量的方法集 package main import "fmt" type Person struct { name string sex byte age int } fun ...
- HDU 1104 Remainder (BFS求最小步数 打印路径)
题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...
- C++/Java中继承关系引发的调用关系详解
C++: 这里引用到了 http://blog.csdn.net/haoel/article/details/1948051/ 中的内容,还请提前阅读陈大神的这篇博客后在阅读本篇. 覆盖,实现多态的基 ...
- Java多线程设计模式(四)
目录(?)[-] Future Pattern Two-Phase Termination Pattern Thread-Specific Storage Pattern Active Object ...
- telnet ip port
windows测试远程端口服务是否能连接上:telnet ip port windows7 系统需要手动启用telnet功能,如下图:
- java-02 JDK安装与环境变量配置&安装编程IDE
1.JDK下载安装与环境变量的配置 1.1 官方JDK 下载地址 大家可以到Oracle中国官方网站下载JDK,也可已 通过这个链接下载 (推荐大家下载1.8版本,这个版本是当前比较流行的版本) 也可 ...
- 7z文件格式及其源码的分析(三)
上一篇在这里. 这是7z文件格式分析的第三篇, 相信有了前两篇的准备,你已经了解了7z源码的大致结构, 以及如何简单调试7z的源码了. 很多同学是不是迫不及待想要拔去7z的神秘外衣,看看究竟了. 好 ...
- C#线程/进程同步(lock、Mutex、Semaphore)
一.lock(实质是Monitor.Enter和Monitor.Exit)(线程同步) 二.Mutex(互斥量)(线程/进程同步) Mutex有个好的特性是,如果程序结束时而互斥锁没通过Release ...