题意:给出杨辉三角的层数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. django1.6之template基础用法

    >>> settings.configure()>>> tem=template.Template("my template is {{name}}&qu ...

  2. Web服务器集群搭建关键步骤纪要

    前言:本文记述了搭建一个小型web服务器集群的过程,由于篇幅所限,系统.软件的安装和基本配置我这里就省略了,只记叙关键配置和脚本内容.假如各位朋友想了解各软件详细配置建议查阅官方文档. 一 需求分析: ...

  3. div中加入iframe,可以实现Ajax的功能

    div中加入iframe,可以实现Ajax的功能,如果查询的时候,比如说城市的选择,用Ajax去实现, 在.net 可以考虑用UpdatePanel,但是点击了查询刷新表格(表格需要重新刷新加载,非纯 ...

  4. javascript高级编程笔记06(面相对象2)

    1)  构造函数模式 es中的构造函数可以用来创建特定类型的对象,像Object和Array这样的原生构造函数,在运行时会自动出现在执行环境中,此外,也可以创建自定义的构造函数,从而定义自定义对象类型 ...

  5. sql中的inner join, left join, right join的区别

    下面介绍一下 inner join, left join, right join这者之间的区别 现在我假设有A表和B表 left join select * from A a left join B ...

  6. 我又回来了,这回是带着C++来的

    一晃就是5年,之前在博客园开这个博客主要是跟着大牛学习C#,那个时候自己偏重于asp.net,后来开发了一段时间的Winform.近几年由于工作原因,偏重于测试仪器开发,属于工控行业,主要使用的是C+ ...

  7. Visual C++ unicode and utf8 转换

    ATL宏: USES_CONVERSION; W2A A2W CString StringUtil::UTF8_to_UNICODE(const char *utf8_string, int leng ...

  8. POJ3282+模拟

    模拟题 /* 模拟 注意:相同一边的车有先后顺序! */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...

  9. Tarjan+模板

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #in ...

  10. mysql-5.7.10-winx64 安装时遇到的问题

    1.修改密码# /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &am ...