【Pascal's Triangle】cpp
题目:
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) {
vector<vector<int> > ret;
if ( numRows< ) return ret;
vector<int> pre, curr;
for ( int i=; i<numRows; ++i )
{
curr.clear();
curr.push_back();
for ( int j=; j<pre.size(); ++j )
{
if ( j==pre.size()- ){
curr.push_back();
}
else{
curr.push_back(pre[j]+pre[j+]);
}
}
pre = curr;
ret.push_back(curr);
}
return ret;
}
};
tips:
数组基本操作。
==============================================
第二次过这道题,代码更简洁了。
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > ret;
if ( numRows< ) return ret;
vector<int> pre;
pre.push_back();
ret.push_back(pre);
for ( int i=; i<numRows; ++i )
{
pre = ret.back();
vector<int> curr;
curr.push_back();
for ( int j=; j<pre.size(); ++j ) curr.push_back(pre[j-]+pre[j]);
curr.push_back();
ret.push_back(curr);
}
return ret;
}
};
===========================================
第三版,更简洁了一些
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> > ret;
if (numRows==) return ret;
vector<int> tmp;
tmp.push_back();
ret.push_back(tmp);
for ( int i=; i<numRows; ++i )
{
for ( int j=tmp.size(); j>; --j ) tmp[j] = tmp[j] + tmp[j-];
tmp.push_back();
ret.push_back(tmp);
}
return ret;
}
};
【Pascal's Triangle】cpp的更多相关文章
- leetcode 【 Pascal's Triangle 】python 实现
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- 【Pascal's Triangle II 】cpp
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode 【 Pascal's Triangle II 】python 实现
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
随机推荐
- python title() upper() lower() 以首字母大写的方式显示每个单词/将字符串改为全部大写或全部小写
以首字母大写的方式显示每个单词 [root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ name ...
- redis hash类型
- javaweb基础(40)_jdbc框架
一.元数据介绍 元数据指的是"数据库"."表"."列"的定义信息. 1.1.DataBaseMetaData元数据 Connection.g ...
- 关于vue中的nextTick深入理解
定义[nextTick.事件循环] nextTick的由来: 由于VUE的数据驱动视图更新,是异步的,即修改数据的当下,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统一进行视图更新 ...
- java基础必备单词讲解 day six
development development development development 开发 development developmentenvironment environment en ...
- java自定义泛型 面试题:接收任意数组进行反转 泛型通配符
不用泛型只能操作某种类型进行反转 代码如下: package com.swift.fanxing; import org.junit.Test; public class RenyiReverse { ...
- BZOJ3288: Mato矩阵(欧拉函数 高斯消元)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 386 Solved: 296[Submit][Status][Discuss] Descriptio ...
- IntelliJ IDEA 2016 汉化说明:
把汉化包解压后,然后将resources_cn.jar 复制到 .lib 目录,重新打开就是中文.
- 关于移动端video标签层级问题
这是在微信中正常页面,就是用了一个原生video标签没做任何处理.然后顶部是固定页面顶端的,这个时候向上滑动页面时,会出现下图现象 这个时候正常人都会想到z-index问题,我也是这样想的,可惜很抱歉 ...
- css 自动换行,超出省略号代替
overflow : hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; ...