[leetcode] 2. 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,3,1]
.Note: Could you optimize your algorithm to use only O(k) extra space?
就是输入k然后输出第k行帕斯卡三角【其实我一直把它叫杨辉三角】。
我这边思路是拿queue来不断弹出压入处理做那个二项式展开:
vector<int> getRow(int rowIndex)
{
int aspros, defteros;
aspros = defteros = ;
queue<int> tmp;
vector<int> Pascal; if (rowIndex == )
{
Pascal.push_back();
return Pascal;
} tmp.push();
tmp.push();
tmp.push(); for (int i = ; i < rowIndex; i++)
{
tmp.push();
do
{
aspros = tmp.front();
if (!tmp.empty())
tmp.pop();
defteros = tmp.front();
tmp.push(aspros + defteros);
} while (defteros != );
}
tmp.pop();
while (!tmp.empty())
{
Pascal.push_back(tmp.front());
tmp.pop();
} return Pascal;
}
queue和vector其实感觉还不熟,应该可以拿vector直接来做的【以后改】
[leetcode] 2. Pascal's Triangle II的更多相关文章
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- LeetCode 119. 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, ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- leetcode 119 Pascal's Triangle II ----- java
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java [Leetcode 119]Pascal's Triangle II
题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- C#解leetcode:119. Pascal's Triangle II
题目是: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- LeetCode(33)-Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
随机推荐
- Linux HDD information (SATA/SCSI/SAS/SSD)
举例一: [reistlin@reistlin.com ~]$ cat /proc/scsi/scsi | grep Model Vendor: ATA Model: OCZ-VERTEX2 3.5 ...
- [转] FTP主动模式和被动模式的区别
转自原文FTP主动模式和被动模式的区别 基础知识: FTP只通过TCP连接,没有用于FTP的UDP组件.FTP不同于其他服务的是它使用了两个端口, 一个数据端口和一个命令端口(或称为控制端口).通常2 ...
- c++builder 画图 填充
c++builder 画图 填充 void __fastcall TForm2::Button1Click(TObject *Sender) { Canvas->Brush->Color ...
- Oracle11gR2导入导出实战之物化视图prebuilt
源实例上创建表 物化视图 oracle@localhost admin]$ sqlplus system/oracle@orcl2 SQL*Plus: Release 11.2.0.4.0 Produ ...
- Proxmox VE 设置备忘
现在PROXMOX 虚拟机一共两个(使用的是N3700 cpu的一个小机器主要为了省电.) 一个是ROS,经过折腾,IK8速度还不错就是资源占用比较大特比下载数据大时对CPU占用很大:OpenWRT不 ...
- iOS学习之第二个View使用UITabBarViewController
前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之 ...
- Mysql事务及行级锁
事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...
- Centos7安装jekyll
1.首先需要安装相应的依赖包及所需要的工具 sudo yum install nodejs npm ruby ruby-devel rubygems git 2.修改gem源 国内 使用的淘宝的更新源 ...
- Centos7.2下编译安装python3.7
1.安装python3.7所需要的依赖. yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel rea ...
- A* 算法求第k短路径
A*算法是一类贪心算法,其可以用于寻找最优路径.我们可以利用A*算法来求第k短路径. 一条路径可以由两部分组成,第一部分是一个从出发到达任意点的任意路径,而第二部分是从第一部分的末端出发,到终点的最短 ...