题意:给出杨辉三角的层数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. java之StringBuffer

    StringBuffer就是字符串缓冲区,用于存储数据的容器. 特点:长度可变,可存储不同类型的数据,最终转化成字符串使用,可以对字符串修改 功能: 添加:append(value), insert( ...

  2. CSS 将按钮转成超链接样式

    一.将按钮转成超链接样式 .GoStyle    {         color: #0c5fc4;                   background-color: #FFFFFF;      ...

  3. 【NHibernate】HQL入门

    在NHibernate 中 HQL 可以帮我们转成最终依赖数据库的查询脚本: 语法也甚是强大,适配主流数据库, HQL不支持union,要想取多个表数据可以做两次单独查询. IQuery query ...

  4. sql shard/partition

    sql http://www.infoq.com/news/2011/02/SQL-Sharding/ http://channel9.msdn.com/Shows/Data-Exposed/SqlD ...

  5. C# send mail with outlook and word mailmerge

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.document_members(v=office.15). ...

  6. C#学习笔记---基础入门(一)

    C#中的变量: 一个变量就是存储区(内存)中的一个存储单元. 变量声明赋值:int money =1000;/int money;money=1000; 输出:console.writeLine(mo ...

  7. HTTP幂等性

    http://www.cnblogs.com/weidagang2046/archive/2011/06/04/2063696.html 理解HTTP幂等性 基于HTTP协议的Web API是时下最为 ...

  8. 1194: [HNOI2006]潘多拉的盒子 - BZOJ

    Description  Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述.每一块的 ...

  9. What is an eigenvector of a covariance matrix?

    What is an eigenvector of a covariance matrix? One of the most intuitive explanations of eigenvector ...

  10. uva 348

    dp还是比较容易  关键是输出路径 .... #include <cstdlib> #include <cstdio> #include <cstring> #de ...