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, ...
随机推荐
- 【算法笔记】B1032 挖掘机技术哪家强
1032 挖掘机技术哪家强 (20 分) 为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 1 ...
- 字典序的第K小数字
今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...
- Codeforces - 617E 年轻人的第一道莫队
我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; ...
- 多个模块使用python logging
链接:https://docs.python.org/2/howto/logging-cookbook.html#logging-cookbook 具体的使用方法,请参考如下代码: import lo ...
- libevent 同性恋 讲解
https://aceld.gitbooks.io/libevent/content/25hong_fa_mo_5f0f_md.html github 这url 干活比较好 https://githu ...
- Android Zygote进程启动分析
dvm,app进程,linux进程三者关系 DVM指 dalivk 的虚拟机.每一个 Android 应用程序都在它自己的进程中运行,都拥有一个独立的 Dalvik 虚拟机实例.而每一个 DVM 都是 ...
- GreenPlum 大数据平台--并行备份(四)
01,并行备份(gp_dump) 1) GP同时备份Master和所有活动的Segment实例 2) 备份消耗的时间与系统中实例的数量没有关系 3) 在Master主机上备份所有DDL文件和GP相关的 ...
- (转)模块readline解析
模块readline解析 原文:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令 ...
- java scoket http TCP udp
http://blog.csdn.net/kongxx/article/details/7259436 TCP/UDP: 齐全:http://www.blogjava.net/Reg/archive/ ...
- unity烘焙记录
1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...