[LeetCode 118] - 杨辉三角形(Pascal's Triangle)
问题
给出变量numRows,生成杨辉三角形的前numRows行。
例如,给出numRows=5,返回:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
初始思路
基本算法和 杨辉三角形II(Pascal's Triangle II) 的基本一致。每算完一行的值将这些值拷贝一份到vector中即可。代码如下:
- class Solution {
- public:
- std::vector<std::vector<int> > generate(int numRows)
- {
- std::vector<std::vector<int> > result;
- std::vector<int> columnInfo;
- if(numRows == )
- {
- return result;
- }
- columnInfo.push_back();
- result.push_back(columnInfo);
- if(numRows == )
- {
- return result;
- }
- columnInfo.push_back();
- for(int i = ; i < numRows; ++i)
- {
- for(int j = i; j > ; --j)
- {
- //第一列和最后一列永远为1,不需要进行处理
- if(j != && j != i)
- {
- columnInfo[j] = columnInfo[j - ] + columnInfo[j];
- }
- }
- result.push_back(columnInfo);
- //下一行开始列数相应增加,且最后一列的数字肯定是1
- columnInfo.push_back();
- }
- return result;
- }
- };
generate
[LeetCode 118] - 杨辉三角形(Pascal's Triangle)的更多相关文章
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 118. 119. Pascal's Triangle -- 杨辉三角形
118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 【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, ...
- LeetCode(118) Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- Leetcode No.119 Pascal's Triangle II(c++实现)
1. 题目 1.1 英文题目 Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's tria ...
- LeetCode OJ 119. 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, ...
随机推荐
- java下载csv文件,中文标题
@RequestMapping(value = "/export.do") public void exportpushuserByareacode(HttpServletRequ ...
- JQuery的ready函数与JS的onload的区别详解
JQuery的ready函数与JS的onload的区别:1.执行时间window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.$(document).ready()是DOM结构绘制 ...
- JAVA中JNI的简单使用
了解JNI:JAVA因其跨平台特性而受人们喜爱,也正因此,使得它和本机各种内部联系变得很少,所以JNI(Java Native Interface)就是用来解决JAVA本地操作的一种方式.JAVA通过 ...
- gulp入门学习
一.gulp简介 gulp是一个自动化构建工具.在开发过工程中,能够使用gulp对项目进行自动构建,大大提高工作效率. 二.安装gulp 在安装gulp之前先要确认已经正确安装了node.js,然后在 ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- BlockingQueue接口
BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会阻塞,当容量为空时取元素操作会阻塞 ...
- poj 1964 Cow Cycling(dp)
/* 一开始想的二维的 只维护第几只牛还有圈数 后来发现每只牛的能量是跟随每个状态的 所以再加一维 f[i][j][k]表示第i只牛 领跑的j全 已经消耗了k体力 转移的话分两类 1.换一只牛领跑 那 ...
- javascript:运动框架
function startMove(obj,json,fnEnd) { clearInterval(obj.timer);//清除定时器 obj.timer=setInterval(function ...
- .net中Web.config文件的基本原理及相关设置
11.7 使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化 ...
- https加密
对称加密 客户端和服务器使用同一把钥匙,加密算法公开 非对称加密 不同钥匙,公钥加密的私钥可以打开 私钥加密的公钥可以打开 HTTPS关键: 1. 要传输的业务数据,使用对称加密. 客户端生成私钥 ...