题意:给出杨辉三角的层数k,返回最后一层。k=0时就是只有一个数字1。

思路:滚动数组计算前一半出来,返回时再复制另一半。简单但是每一句都挺长的。

0ms的版本:

 class Solution {
public:
vector<int> getRow(int rowIndex) {
if(rowIndex==) return vector<int>(,); //0和1特殊处理
if(rowIndex==) return vector<int>(,);
vector<int> ans[];
ans[].push_back(); //只需要处理一半,另一半在返回时复制。
for(int i=; i<=rowIndex; i++)
{
ans[~i&].clear(); //滚动数组
ans[~i&].push_back(); //这是必须的
int j=;
for(; j<ans[i&].size(); j++) ans[~i&].push_back( ans[i&][j-]+ans[i&][j]); if(i%==) ans[~i&].push_back( ans[i&][j-]+ans[i&][j-] ); //k为偶数时,里面有奇数个呢。
} ans[rowIndex&].clear();
ans[rowIndex&].insert( ans[rowIndex&].end(),ans[~rowIndex&].begin(), ans[~rowIndex&].end() ); if(!(rowIndex&))
ans[rowIndex&].insert( ans[rowIndex&].end(),ans[~rowIndex&].rbegin()+, ans[~rowIndex&].rend() );//总数为奇数个,最后1个不要复制进去。
else
ans[rowIndex&].insert( ans[rowIndex&].end(), ans[~rowIndex&].rbegin(), ans[~rowIndex&].rend() );//偶数个,全复制。
return ans[rowIndex&];
}
};

AC代码

简洁但4ms的版本:

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+,);
for(int i=; i<rowIndex; i++) //正在产生第i+2行。
{
for(int j=i; j>; j--) //必须从右开始,不然前面行就被覆盖了。
{
ans[j]+=ans[j-];
}
}
return ans;
}
};

AC代码

LeetCode Pascal's Triangle II (杨辉三角)的更多相关文章

  1. [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, ...

  2. [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 ...

  3. [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, ...

  4. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. LeetCode(119):杨辉三角 II

    Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  8. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  9. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

随机推荐

  1. AWS--EC2基本概念

    原文:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html EC2:Elastic Compute Cloud 特性包括: ...

  2. Sybase 导入导出命令

    BCP命令详解 BCP是SYBASE公司提供专门用于数据库表一级数据备份的工具. 一般存放在所安装的ASE或者Open Client 的BIN目录中. 可执行文件名称为bcp.EXE 参数列表如下:( ...

  3. ubuntu find方法

    通用格式:find pathname -options [-print -exec -ok]例子:find / -name filename 再根目录里面搜索文件名为filename的文件find / ...

  4. EXTJS store 某行某列数据更新等操作

    1.可以使用add(Ext.data.Record[] records)或者add(Ext.data.Record record)向store末尾添加一个或多个record.如: var newRec ...

  5. (转载)Cocos2dx-OpenGL ES2.0教程:使用VBO索引(4)

    在上一篇文章中,我们介绍了uniform和模型-视图-投影变换,相信大家对于OpenGL ES 2.0应该有一点感觉了.在这篇文章中,我们不再画三角形了,改为画四边形.下篇教程,我们就可以画立方体了, ...

  6. DNF技能贴图的研究

    一直在猜想DNF的技能贴图怎么贴的,靠在游戏里慢慢移动确定技能的偏移太费时间了.前段发现了“可视坐标生成”这软件,针对DNF改衣服,装备款式的小工具,就自己写了个类似的. 从图上看,技能的域中心点和人 ...

  7. submit与button区别提交区别

    提交表单时使用submit会自动提交form表单数据, 如果使用jquery的form表单插件时需要将提交按钮改为button时$("#表单id").ajaxSubmit({}); ...

  8. Python之print语句

    print语句可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' 注意: 1.当我们在Python交 ...

  9. 微信支付-b

    微信支付 APP端开发步骤(传送门):https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5 1.首先下载最新的微信支付的SDK包 ...

  10. unity手游之聊天SDK集成与使用二

    集成思路 如果是自己的小游戏的话,可以把好友等信息直接保存在亲加服务器上,通过调用api来操作. 我们游戏只使用sdk的通信功能,好友等信息保存在自己的服务器上. 用户在登陆游戏的时候,通过算法用用户 ...