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

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

  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. vector<vector<int> > generate(int numRows) {
  8. vector<vector<int>> res;
  9. if (numRows == ) return res;
  10. for (int i = ; i < numRows; i++)
  11. {
  12. vector<int> v; v.clear();
  13. for (int j = ; j <= i; j++)
  14. {
  15. if (j == || j == i) v.push_back();
  16. if (i> && j > && j < i)
  17. {
  18. v.push_back(res[i - ][j] + res[i - ][j - ]);
  19. }
  20. }
  21. res.push_back(v);
  22. }
  23. return res;///////////忘了返回了,一直找不出错来。
  24. }
  25. vector<int> getRow(int rowIndex) {
  26. vector<int> res(rowIndex+,);
  27. vector<int> tmp(,);
  28. //if (rowIndex == 0) return vector<int>(1,1);
  29. for (int i = ; i <= rowIndex; i++)
  30. {
  31. tmp[] = ; tmp[] = ;//别放错位置。之前放到内层的for里了。
  32. for (int j = ; j <= i; j++)
  33. {
  34. if (j == || j == i)
  35. res[j] = ;
  36. if (j> && j < i)
  37. {
  38. if (j % == )
  39. tmp[] = res[j];
  40. else if (j % == )
  41. tmp[] = res[j];
  42. res[j] += tmp[-j%];
  43. }
  44. }
  45. }
  46. return res;
  47. }
  48. };
  49.  
  50. void printVV(vector<vector<int>> vv)
  51. {
  52. for (int i = ; i < vv.size(); i++)
  53. {
  54. for (int j = ; j < vv[i].size(); j++)
  55. {
  56. cout << vv[i][j] << " ";
  57. }
  58. cout << endl;
  59. }
  60. }
  61. int main()
  62. {
  63. Solution s;
  64. //vector<vector<int>> vv = s.generate(3);
  65. vector<int> v = s.getRow();
  66. for (int i = ; i < v.size(); i++)
  67. {
  68. cout << v[i] << " ";
  69. }
  70. //printVV(vv);
  71. return ;
  72. }

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. 【算法笔记】B1032 挖掘机技术哪家强

    1032 挖掘机技术哪家强 (20 分) 为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 1 ...

  2. 字典序的第K小数字

    今天zyb参加一场面试,面试官听说zyb是ACMer之后立马抛出了一道算法题给zyb:有一个序列,是1到n的一种排列,排列的顺序是字典序小的在前,那么第k个数字是什么?例如n=15,k=7, 排列顺序 ...

  3. Codeforces - 617E 年轻人的第一道莫队

    我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧 cnt[i]表示i出现的次数 maxn开2e6比较保险 /*H E A D*/ struct Query{ int l,r,id; ...

  4. 多个模块使用python logging

    链接:https://docs.python.org/2/howto/logging-cookbook.html#logging-cookbook 具体的使用方法,请参考如下代码: import lo ...

  5. libevent 同性恋 讲解

    https://aceld.gitbooks.io/libevent/content/25hong_fa_mo_5f0f_md.html github 这url 干活比较好 https://githu ...

  6. Android Zygote进程启动分析

    dvm,app进程,linux进程三者关系 DVM指 dalivk 的虚拟机.每一个 Android 应用程序都在它自己的进程中运行,都拥有一个独立的 Dalvik 虚拟机实例.而每一个 DVM 都是 ...

  7. GreenPlum 大数据平台--并行备份(四)

    01,并行备份(gp_dump) 1) GP同时备份Master和所有活动的Segment实例 2) 备份消耗的时间与系统中实例的数量没有关系 3) 在Master主机上备份所有DDL文件和GP相关的 ...

  8. (转)模块readline解析

    模块readline解析 原文:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令 ...

  9. java scoket http TCP udp

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

  10. unity烘焙记录

    1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...