【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路:杨辉三角,直接按规律生成即可
vector<vector<int> > generate(int numRows) {
        vector<vector<int>> ans;
        for(int i = ; i < numRows; i++)
        {
            vector<int> v(i + , );
            for(int j = ; j < i; j++)
            {
                v[j] = ans[i - ][j - ] + ans[i - ][j];
            }
            ans.push_back(v);
        }
        return ans;
    }
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
思路:
要靠数学公式了,设杨辉三角的最顶层为第0行,每行的第一个数字是第0个,则
第 i 行第 j 个元素的计算为:C(i, j) = (i)! / (j)! * (i - j)!
那么第 i 行第 j 个元素和它前一个元素的关系是 ans[i][j] = ans[i][j - 1] * (i - j + 1) / j; //这里要注意不要越界
vector<int> getRow(int rowIndex) {
        vector<int> ans(rowIndex + , );
        ans[rowIndex - ] = ans[] = rowIndex;
        for(int i = ; i < rowIndex /  + ; i++)
        {
            ans[rowIndex - i] = ans[i] = (long long)ans[i - ] * (rowIndex - i + ) / i; //用long long防止越界 同时利用杨辉三角的对称性减少一半的计算量
        }
        return ans;
    }
【leetcode】Pascal's Triangle I & II (middle)的更多相关文章
- 【leetcode】House Robber  & House Robber  II(middle)
		You are a professional robber planning to rob houses along a street. Each house has a certain amount ... 
- 【leetcode】Binary Tree Right Side View(middle)
		Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ... 
- 【leetcode】Bitwise AND of Numbers Range(middle)
		Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ... 
- 【leetcode】Longest Substring Without Repeating Characters (middle)
		Given a string, find the length of the longest substring without repeating characters. For example, ... 
- 【LeetCode】Pascal's Triangle II 解题报告
		[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ... 
- 【LeetCode】385. Mini Parser 解题报告(Python)
		[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ... 
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
		[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ... 
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
		[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ... 
- 【LeetCode】911. Online Election 解题报告(Python)
		[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ... 
随机推荐
- Web前端开发规范文档(google规范)
			(Xee:其实没什么规范约束,但是养成一种好习惯,何乐而不为?) 区分大小写 xhtml 区分大小写,xhtml要求 标签名 属性名 值都要小写,并且要有双引号和 标签闭合. css 元素名称以及i ... 
- 统计学 nested_design 嵌套设计
			nested_design 嵌套设计 li_volleyball ,邓邦良 2016年3月6日 嵌套设计 一.基本概念 嵌套设计(nested design)又称为窝设计和套设计,与析因设计的处理不同 ... 
- nyoj 10  skiing 搜索+动归
			整整两天了,都打不开网页,是不是我提交的次数太多了? nyoj 10: #include<stdio.h> #include<string.h> ][],b[][]; int ... 
- PHP基础封装简单的MysqliHelper类
			MysqliHelper.class.php 1: <?php 2: 3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ... 
- mybatis批量插入数据到oracle
			mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“ 错误解决方法 oracle批量插入使用 insert a ... 
- jQuery的$.ajax示例
			$.ajax({ url: 'index.php?module=products&submod=product_experience_manage&method=ajaxGetSele ... 
- JSP自定义标签之Hello Costom tag小例子
			1.项目结构 2.实现自定义tag所需依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId ... 
- iOS开发——高级篇——iOS 中的 NSTimer
			以前的老代码在使用 NSTimer 时出现了内存泄露 NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: 1 2 3 ... 
- Android中的“再按一次返回键退出程序”实现
			用户退出应用前给出一个提示是很有必要的,因为可能是用户并不真的想退出,而只是一不小心按下了返回键,大部分应用的做法是在应用退出去前给出一个Dialog,我觉得这样不太友好,用户还得移动手指去按dial ... 
- android oom 全解析
			Android oom 有时出现很频繁,这一般不是Android设计的问题,一般是我们的问题. 就我的经验而言,出现oom,无非主要是以下几个方面: 一.加载对象过大 二.相应资源过多,没有来不及释放 ... 
