对于第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. BZOJ 5168 && Luogu P3740 [HAOI2014]贴海报 线段树~~

    据说某谷数据十分水...但幸好BZOJ上也过了...话说我记得讲课时讲的是奇奇怪怪的离散化..但现在突然觉得什么都可以线段树瞎搞了...QAQ 直接就是这个区间有没有被覆盖,被覆盖直接return: ...

  2. 1149 Dangerous Goods Packaging (25 分)

    When shipping goods with containers, we have to be careful not to pack some incompatible goods into ...

  3. ESP8266使用详解

    [From] http://www.cnblogs.com/yangfengwu/p/5205570.html 用的这款 各引脚功能:来至厂家提供的资料 GPIO0 默认是工作模式(不接线).如果接了 ...

  4. VUE 入门教程

    http://www.runoob.com/w3cnote/vue-js-quickstart.html VUE安装教程 https://segmentfault.com/a/119000001218 ...

  5. PIE SDK图层树伙伴控件示例

    1.  功能简介 TocControl控件的主要作用是显示当前加载的图层有哪些.采用什么样的符号等,目的是使用户对当前加载的数据和结构有一个总体的把握.与之相关联的伙伴控件有MapControl,Pa ...

  6. opencv java小应用:比较两个图片的相似度

    package com.company; import org.opencv.core.*; import org.opencv.imgcodecs.Imgcodecs; import org.ope ...

  7. Win10小娜关闭或删除进程

    先来说下我为什么想尽方法关闭win10小娜:我觉得功能并不适用于我,即便不启用Cortana小娜,在Win10进程中也会看到Cortana小娜启动着,耗费了内存.CPU,而且主要的我的磁盘利用率等都居 ...

  8. python 爬虫系列06--古诗文

    读书破万卷,下笔如有神 import requests import re def parse_page(url): headers = { 'USer-Agent':'user-agent: Moz ...

  9. DP Intro - Tree DP Examples

    因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...

  10. java scoket http TCP udp

    http://blog.csdn.net/kongxx/article/details/7259436 TCP/UDP: 齐全:http://www.blogjava.net/Reg/archive/ ...