对于第2个pascal triangle,通过观察可以发现,其实只需要2个额外的变量来记录,于是就设了个tmp数组。

整体有点DP问题中的滚动数组的感觉。

 #include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> res;
if (numRows == ) return res;
for (int i = ; i < numRows; i++)
{
vector<int> v; v.clear();
for (int j = ; j <= i; j++)
{
if (j == || j == i) v.push_back();
if (i> && j > && j < i)
{
v.push_back(res[i - ][j] + res[i - ][j - ]);
}
}
res.push_back(v);
}
return res;///////////忘了返回了,一直找不出错来。
}
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex+,);
vector<int> tmp(,);
//if (rowIndex == 0) return vector<int>(1,1);
for (int i = ; i <= rowIndex; i++)
{
tmp[] = ; tmp[] = ;//别放错位置。之前放到内层的for里了。
for (int j = ; j <= i; j++)
{
if (j == || j == i)
res[j] = ;
if (j> && j < i)
{
if (j % == )
tmp[] = res[j];
else if (j % == )
tmp[] = res[j];
res[j] += tmp[-j%];
}
}
}
return res;
}
}; void printVV(vector<vector<int>> vv)
{
for (int i = ; i < vv.size(); i++)
{
for (int j = ; j < vv[i].size(); j++)
{
cout << vv[i][j] << " ";
}
cout << endl;
}
}
int main()
{
Solution s;
//vector<vector<int>> vv = s.generate(3);
vector<int> v = s.getRow();
for (int i = ; i < v.size(); i++)
{
cout << v[i] << " ";
}
//printVV(vv);
return ;
}

leetcode-pascal triangle I&&II的更多相关文章

  1. leetcode—pascal triangle

    1.题目描述 Given numRows, generate the first numRows of Pascal's triangle.   For example, given numRows ...

  2. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  3. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

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

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

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

随机推荐

  1. python之常用的数据类型

    1. 变量的定义以及声明 在学习变量之前,咱们需要知道变量的命名规则: ① 变量必须由数字字母下划线构成,如a_1 ② 变量名不能以数字开头,如1a ③ 需要遵循驼峰命名法 给变量赋值通常采用“=”, ...

  2. dataTable 加了竖向滚动条导致列头样式错位的问题 / 亲测可用,不好用你打我,用好了记得点推荐

    tab在没有显示之前,容器是没有高度宽度的,而dt在自动计算高度和宽度时是获取的外部容器的高度和宽度,当切换tab时,dt获取不到这个高度宽度,导致列头都挤在一起,是用下面代码解决此问题 $('a[d ...

  3. Firefox与IE浏览器缓存的两个重要区别

    转自: http://www.yeeyan.org/articles/view/mouse4x/17150 当你建立好一个WEB服务后,通常有两个类型的缓存需要配置: 设置网站有更新的时候html资源 ...

  4. Maven系统学习

    1. 1.1 何为构建? 编译.测试.运行.打包.部署等工作: Maven就是用软件的办法让这一系列工作自动化,只需要一条简单的命令,所有繁琐的工作就会自动完成: Maven最大的消除了构建的重复,抽 ...

  5. spark ALS 推荐算法参数说明

  6. (转)网站速度优化技巧:Nginx设置js、css过期时间

    网站速度优化技巧:Nginx设置js.css过期时间 原文:http://www.webkaka.com/blog/archives/Nginx-set-the-expiration-time-for ...

  7. IDEA 14.0 (默认模式) 快捷键

    IDEA 14.0 (默认模式) 快捷键 1.Alt+Shift+R:重命名变量名.类名.方法名(使用已经使用过的) 2.Ctrl+O :重写方法 3.Alt+Shift+P :实现接口 4.Alt+ ...

  8. ul+js模拟select

    html   css .select_box{ float: left; } .select_box input{ width: 160px; height: 30px; text-align: ce ...

  9. C#博客记录一

    前言:C#语言是由微软公司开发面向大众的一款软件开发语言. 1.c语音的输出语句为Console.Write();和Console.WriteLine(); 两者区别为后者为换行输出,前者不换行. 2 ...

  10. hadoop 天气案例

    对下面一组气温数据进行处理,得到每个月份最高的两个气温值 2018-12-12 14:30 25c2018-12-12 15:30 26c2017-12-12 12:30 36c2019-01-01 ...