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,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

输出Pascal三角形的第k行(从0开始);空间复杂度为O(k);

从第0行开始生成,每次利用已经生成的行,在原有空间上生成下一行,直到生成所要求的行。

代码入下:

 class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> pascal;
pascal.push_back();
int m = ;
for(int i = ; i <= rowIndex; i++)
{
if(rowIndex == )
{
return pascal;
}
for(int j = ; j < i+; j++)
{
if(j == i)
{
pascal.push_back();
break;
}
int n = pascal[j];
pascal[j] = m + n;
m = n;
}
}
return pascal;
}
};

leetcode 119的更多相关文章

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

  2. [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)

    问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...

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

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

  5. Java实现 LeetCode 119 杨辉三角 II

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

  6. 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 ...

  7. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

  8. 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, ...

  9. 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 ...

随机推荐

  1. git push失败的解决办法(2)

    错误一:Cannot rebase: You have unstaged changes 解决办法: Cannot rebase: You have unstaged changes. 那说明有修改过 ...

  2. 加密--win7下安装openssl

    http://www.cnblogs.com/ZhouL3777/archive/2012/10/21/2732890.html http://www.cnblogs.com/ZhouL3777/ar ...

  3. 区分listview的item和Button的点击事件

    这两天修改领导通的ListView widget,在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了, 后来在网上查资料 ...

  4. 时间c#

    无论Time.timeScale 等于多说Update和LateUpdate都会去执行 Time.timeScale会影响FixedUpdate的速度.  Time.timeScale还会影响Time ...

  5. C#里WinForm开发中如何实现控件随窗体大小的改变而自动适应其改变(转)

    在设计可供用户调整大小的窗体时,如何实现该窗体上的控件也应能正确地随窗体的改变而自动调整大小并且能重新定位?此时就要借助控件的.Anchor属性.Anchor属性定义控件的定位点位置.当控件锚定到某个 ...

  6. 服务器网页GZIP压缩怎么配置

    服务器网页GZIP压缩怎么配置     服务器网页GZIP压缩怎么配置,GZIP压缩对网页压缩来说最好不过了,下面是IIS下Gzip配置详细操作步骤:     简单来说,IIS6集成了Gzip,只不过 ...

  7. [ActionScript3.0] 传递任意数量的参数

    function setAgument(a:int,...rest):void{ for(var i:int=0;i<rest.length;i++){ trace(rest[i]); }}se ...

  8. crm 4 UserHasRole

    //获取当前人员是否含有指定角色权限 function UserHasRole(roleName) { //get Current User Roles, oXml is an object var ...

  9. lassen项目启动

    1.将代码从svn下载下来 2.在下载目录打开命令窗口 shift+鼠标右键 3.mvn  clean install mvn  eclipse:clean mvn  eclipse:eclipse

  10. POJ 1611

    菜鸟第一次做这种.想了好一会儿.== 首先还是初始化记忆数组,使得每一个元素的初始根节点是自己. 然后是对输入的数据进行并集.我们拿出每组元素的第一个作为根节点. 每次检测是否已经存在根节点.如果存在 ...