LeetCode118: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]
]
Subscribe to see which companies asked this question
//解题思路:利用一个中间vector来保存每层的数
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> ans;
for(int i = 0;i < numRows;i++)
{
vector<int> cur;
if(i == 0)
cur.push_back(1);
else
{
for(int j = 0;j <= i;j++)
{
if(j == 0 || j == i) cur.push_back(1);
else cur.push_back(ans[i - 1][j] + ans[i - 1][j - 1]);
}
}
ans.push_back(cur);
} return ans;
}
};
LeetCode118:Pascal's Triangle的更多相关文章
- 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 ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 【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 ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Pascal's Triangle I,II
题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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 ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
随机推荐
- 好用的批量改名工具——文件批量改名工具V2.0 绿色版
我找了一个绿色免安装的软件来实现批量改名要求 下载地址:http://www.orsoon.com/Soft/14049.html#xiazai 添加图片后,开始改名.通过输入a#就可以将这些图片进行 ...
- EditText中文文档
感谢农民伯伯的翻译文:http://www.cnblogs.com/over140/archive/2010/09/02/1815439.html 属性名称 描述 android:autoLink 设 ...
- Dropout原理解析
1. Dropout简介 1.1 Dropout出现的原因 在机器学习的模型中,如果模型的参数太多,而训练样本又太少,训练出来的模型很容易产生过拟合的现象.在训练神经网络的时候经常会遇到过拟合的问题, ...
- Docker 常用命令与操作
介绍 此命令集合版本为 1.11.1 及以上 基础类 查看docker信息 # 查看docker版本 docker version # 显示docker系统的信息 docker info # 日志信息 ...
- Forward secrecy
In cryptography, forward secrecy (FS), also known as perfect forward secrecy (PFS), is a property of ...
- FinalizableReference, FinalizablePhantomReference, FinalizableReferenceQueue
FinalizableReference /* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License ...
- jvm垃圾回收原理(转)
原文链接:jvm垃圾回收原理 在jvm中堆空间划分为三个代:年轻代(Young Generation).年老代(Old Generation)和永久代(Permanent Generation).年轻 ...
- $.getJSON的缓存问题处理
今天遇到jQuery.getJSON的缓存问题.如果其调用的url之前曾经调用过的话,回调函数就会直接在缓存里面取得想要得值,而不是进入到后台,调用存储过程了.这是一个比较郁闷的问题.不修改的话,用户 ...
- Lync 2013安装中遇到的关于SQL Mirroring的一次报错的解决
Problem Description ================= Following the Lync Deployment Wizard to setup Database Mirrori ...
- Centos 7 开放查看端口 防火墙关闭打开
Centos 7 firewall 命令: 查看已经开放的端口: firewall-cmd --list-ports 开启端口 firewall-cmd --zone=public --add-por ...