leetcode-pascal triangle I&&II
对于第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的更多相关文章
- leetcode—pascal triangle
1.题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- [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, ...
随机推荐
- SSH 项目建立过程
1. 加入 Spring 1). 加入 jar 包 2). 配置 web.xml 文件 <context-param> <param-name>contextConfigLoc ...
- window.open打开窗口的几种方式
1. 在当前窗口打开百度,并且使URL地址出现在搜索栏中. window.open("http://www.baidu.com/", "_search"); w ...
- GreenPlum 大数据平台--web监控
一,安装web监控界面 01,准备 下载greenplum cc包,>> 解压缩 02,安装前配置 vim /greenplum/data/master/gpseg-/pg_hba.con ...
- 安装wine
sudo add-apt-repository ppa:ubuntu-wine/ppa sudo apt-get update sudo apt-get install winetricks
- CSS像素、设备独立像素、设备像素之间关系
CSS像素.设备独立像素.设备像素,三者联系紧密又有很大的区别,而我们主要是在做移动端开发的时候需要更多地用到这些概念,那他们分别是指什么呢? 概念 CSS像素(CSS Pixel):适用于web编程 ...
- The Definitive C++ Book Guide and List--reference
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Reference Style - All ...
- C#动态方法调用 提高程序的扩展性
此篇将介绍C#如何在运行时动态调用方法.当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的.此篇则给你一把利剑,让动态对象的方法调用成为可能. 1.动态调用dll里的 ...
- 删除table表格行
function getRowObj(obj) { while(obj.tagName.toLowerCase()!="tr") // toLowerCase转化小写 { ...
- ASP.NET内置对象-网页转向的4中方法
ASP.NET页面跳转的方法 1.超链接 2.Response.Redirect(“Urlstring”) 3.Server.Transfer(“UrlString”) 4.PostBackUrl 1 ...
- 嵌入式Tomcat Web服务器的使用
在运行web工程时,常常要频繁启动tomcat,使用嵌入式tomcat可以减少部分重复操作. 1.下载tomcat5.0.28embed.zip 解压文件夹复制到工程下. http://archive ...