Description

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,

Return [1,3,3,1].

my program

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res = {1};
for(int i = 1; i<=rowIndex; i++)
{
res.push_back(0);
for(int j = res.size(); j>0; j--)
{
res[j] += res[j-1];
}
}
return res;
}
};

34 / 34 test cases passed.

Status: Accepted

Runtime: 0 ms

other methods

vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1,1);
int small = rowIndex/2;
long comb = 1;
int j = 1;
for (int i=rowIndex; i>=small; i--){
comb *= i;
comb /= j;
j ++;
ans[i-1] = (int)comb;
ans[j-1] = (int)comb;
}
return ans;
}

LeetCode119. Pascal's Triangle II的更多相关文章

  1. 每天一道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, ...

  2. [LeetCode119]Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  3. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  4. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  5. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  6. 杨辉三角形II(Pascal's Triangle II)

    杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...

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

  8. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

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

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

随机推荐

  1. IO流--File--properties

    package com.songyan.properties; /** * properties * 是hashtable的子类具备map集合的特点 * 里面存储的键值对都是String而且不需要指定 ...

  2. 【java】乱码处理+编码转化+判断字符串编码方式

    之前有一篇是修改IDE的编码,服务器的编码等处理乱码,但是在所有环境因素上,保证了编码方式之后,也会有前台传递给后台[get方式提交]传递给后台的编码方式是非UTF-8的,也会有例如FTP服务器的编码 ...

  3. easyui datagrid加载成功之后选定并获取首行数据

    //加载成功之后,选定并获取首行数据 onLoadSuccess:function(data){ alert("grid加载成功"); var rows=$('test').dat ...

  4. grpc(3):使用 golang 开发 grpc 服务端和client

    1,关于grpc-go golang 能够能够做grpc的服务端和client. 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://g ...

  5. HDU 4289 Control (最小割 拆点)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  6. Python连接MySQL乱码(中文变问号)

    #coding=utf-8 import MySQLdb db = MySQLdb.connect("IP","用户名","密码",&quo ...

  7. win8 推送通知 小记

    http://blog.csdn.net/nacl025/article/details/8998552 http://blog.csdn.net/nacl025/article/details/90 ...

  8. unity Chan!下载

    http://unity-chan.com/download/datadownload.html

  9. JavaScript完整性检查

    1.7个“坑” <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...

  10. 对象内部属性[[Class]]

    1.概述 所有的typeof返回值为‘object’的对象都包含一个内部属性[[Class]],我们将它可以看做内部的分类,而非传统面向对象意义的分类.这个属性无法直接访问,一般通过Object.pr ...