1. 程式人生 > >119. Pascal's Triangle II(楊輝三角簡單變形)

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 that the row index starts from 0.

(翻譯:給定一個非負索引 k,其中 k ≤ 33,返回楊輝三角的第 行。)

In Pascal's triangle, each number is the sum of the two numbers directly above it.

(在楊輝三角中,每個數是它左上方和右上方的數的和。)

Example:

Input: 3
Output: [1,3,3,1]


【分析】

只是我上一篇部落格題目的簡單變形,此題更為簡單,還是用ArrayList + 暴力求解,Java實現程式碼如下:

class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> row = new ArrayList<>();
        if (rowIndex < 0) return row;
        for (int i = 0; i < rowIndex + 1; i++) {
            row.add(0, 1);
            for (int j = 1; j < row.size() - 1; j++) {
                row.set(j, row.get(j) + row.get(j + 1));
            }
        }
        return row;
    }
}

相關推薦

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 that the row index starts from

Pascal's Triangle II 生成三角中的某行

題目: 連結 解答: 在上一題基礎上修改,注意行號。 程式碼: class Solution { public: vector<int> getRow(int rowIndex) { vector<vector<int> > res

pascals triangle ii三角、帕斯卡三角

題目描述 Given an index k, return the k th row of the Pascal’s triangle. For example, given k = 3, Return[1,3,3,1]. Note: Could you optimize your

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

LeetCode(Python版)——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 that the row index starts from 0. In

Combinatorics——HDUOJ 1799 - 迴圈多少次?三角 - 排列組合

原題: Problem Description 我們知道,在程式設計中,我們時常需要考慮到時間複雜度,特別是對於迴圈的部分。例如, 如果程式碼中出現 for( i=1; i<=n; i++) OP ; 那麼做了n次OP運算,如果程式

Recursive sequence 矩陣快速冪 + 組合數 非線性變線性,利用到了組合數三角求解快

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a

ACM程式設計選修課——1031: Hungar的得分問題(二)三角+二進位制轉換

1031: Hungar的得分問題(二) 時間限制: 1 Sec  記憶體限制: 64 MB 提交: 15  解決: 10 [提交][狀態][討論版] 題目描述 距離正式選秀時間越來越近了,今天H

LeetCode 118. Pascal's Triangle 三角

== pascal else 只需要 first [1] blog 日期 都是 Given numRows, generate the first numRows of Pascal‘s triangle. For example, given numRows = 5,R

[LeetCode] Pascal's Triangle II 三角之二

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. In Pascal's triang

java演算法之簡單的帕斯卡三角Pascals Triangle

轉載自:http://blog.csdn.net/ylyg050518/article/details/48517151 今天這道題目是也是一個經典的問題,列印Pascal’s Triangle,(帕斯卡三角或者說是楊輝三角)。 問題描述  Given numRo

LeetCode | Pascal's Triangle三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,

Pascal's Triangle II:節省空間三角

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

LeetCode演算法題-Pascal's Triangle IIJava實現

這是悅樂書的第171次更新,第173篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第30題(順位題號是119)。給定非負索引k,其中k≤33,返回Pascal三角形的第k個索引行。行索引從0開始。在Pascal的三角形中,每個數字是它上面兩個數字的總和。例如: 輸

【c/c++】leetcode 118 & 119 Pascals triangle I & II

 1 普通思路 根據定義,用一個vector存前一行的資料,後一行根據前一行計算 存一個二維vec 並返回 //118 class Solution { public: vector<vector<int>> generate(int numRo

Pascal triangle 三角製表符

Description: By using two-dimensional array, write C program to display a table that represents a Pascal triangle of any size. In

【LeetCode】140.Pascal's Triangle II

題目描述(Easy) Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts

LeetCode Pascal's Triangle II

Problem Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note:

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,3,1]. Note: Could you optimi