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?

 
思路:从右往左遍历并填写三角形的图结构

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> a(rowIndex + ); a[] = ;
for(int i = ; i <= rowIndex; i++) //遍历行
for(int j = i; j >= ; j--) //遍历行中的元素,第k行有k个元素。注意行中元素需要从右往左遍历,否则下一行填写的结果会覆盖上一行还未使用的元素
if (j == i)
a[j] = a[j-];
else if (j == )
a[j] = a[j];
else
a[j] = a[j-] + a[j]; return a;
}
};

119. Pascal's Triangle II (Graph; WFS)的更多相关文章

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

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

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

  3. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  4. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

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

  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]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  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. vue.js 源代码学习笔记 ----- text-parse.js

    /* @flow */ import { cached } from 'shared/util' import { parseFilters } from './filter-parser' //找到 ...

  2. SQLSERVER 数据调度示例,调度数据到中间表或者历史表

    USE [MeiDongPay_Test] GO /****** Object: StoredProcedure [dbo].[Job_BatchTransferOrderToMidst] Scrip ...

  3. leetcode122 买卖股票的最佳时机 python

    题目:给定一个数组,它表示了一只股票的价格浮动,第i个元素代表的是股票第i天的价格.设计一个函数,计算出该股票的最大收益,注意,可以多次买入卖出,但下一次买入必须是在本次持有股票卖出之后.比如[1,7 ...

  4. 【网络通信】TCP三次握手和四次挥手的示意图

    三次握手 TCP连接是通过三次握手来连接的. 第一次握手 当客户端向服务器发起连接请求时,客户端会发送同步序列标号SYN到服务器,在这里我们设SYN为m,等待服务器确认,这时客户端的状态为SYN_SE ...

  5. linuxkit 基本试用

    安装 linux go get -u github.com/linuxkit/linuxkit/src/cmd/linuxkit mac brew tap linuxkit/linuxkit brew ...

  6. 使用bit管理npm包

    npm 包给共享,团队写作有好多问题需要解决,类似的解决方案有lerna yarn 使用monorepo,bit 官方有相关的比较 以下只说明如何进行简单的项目使用 网站 https://bitsrc ...

  7. Java基础之一:Java开发环境配置

    Java 开发环境配置 window系统安装java 下载JDK,地址:http://www.oracle.com 在下载页面中你需要选择接受许可,并根据自己的系统选择对应的版本,本文以 Window ...

  8. FastAdmin bootstrap-table 分页手动输入跳转

    FastAdmin bootstrap-table 分页手动输入跳转 Bootstrap-Table (V1.11.0)默认是没有这个功能的,不过作者有写的扩展. https://github.com ...

  9. 【转】vim环境设置和自动对齐

    原文网址:http://blog.chinaunix.net/uid-23525659-id-4340245.html 注:如果是用vim编写代码,建议开启vim的文件类型自动检测功能,这样编写代码换 ...

  10. postgresql双机热备、高可用方案(采用pacemaker+corosync实现)

    http://blog.csdn.net/qguanri/article/details/51151974 需求描述 我们有两台centos7的数据库主机A.B.要对A.B实现双机热备,A作为数据库m ...