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 optimize your algorithm to use only O(k) extra space
題意:返回揚輝三角第K行數,從0開始記
最笨的想法:
用兩個陣列,計算時用到上次陣列,程式碼如下:結果是通過的
public List<Integer> getRow(int rowIndex) { List<Integer> list = new ArrayList<>(); if (rowIndex == 0) { list.add(new Integer(1)); return list; } else if (rowIndex == 1) { list.add(new Integer(1)); list.add(new Integer(1)); return list; } else { List<Integer> oldList = new ArrayList<>(); //從第2行開始 oldList.add(new Integer(1)); oldList.add(new Integer(1)); for (int i = 1; i <= rowIndex; i++) { list = new ArrayList<>(); list.add(new Integer(1)); for (int j = 1; j < i ; j++) { list.add(oldList.get(j) + oldList.get(j -1)); } list.add(new Integer(1)); oldList = list; } return list; } }
相關推薦
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
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 119. Pascal's Triangle II(楊輝三角II) python3 兩種思路(老土但高效的list拼接 / 優雅的map()方法)
class Solution: def getRow(self, rowIndex): """ :type rowIndex: int :rtyp
[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
【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(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
leetcode-Pascal's Triangle II
和Pascal's Triangle I差不多的思想。 public class Solution { public List<Integer> getRow(int rowIndex) { ArrayList<Integer&g
leetcode 118. Pascal's Triangle (ArrayList、List與思路難)
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle. In Pascal’s tr
LeetCode 118. Pascal's Triangle
分析 題目 Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle. 帕斯卡三角形
leetcode 118. Pascal's Triangle(python3)楊輝三角
題目:楊輝三角 題目分析: 楊輝三角,第一個第二組數的值由第一組數的值決定,例如,x[2][1]=x[1][0]+x[1][1] 既:2=1+1 程式設計思路: 1.題目給出輸入為一個
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
leetcode-118. Pascal's Triangle
func generate(numRows int) [][]int { var arr [][] int; arr = make([][]int, numRows); va
Pascal's Triangle II 生成楊輝三角中的某行
題目: 連結 解答: 在上一題基礎上修改,注意行號。 程式碼: class Solution { public: vector<int> getRow(int rowIndex) { vector<vector<int> > res
leetcode 118 Pascal's Triangle(楊輝三角) python3 兩種解法(清晰的分類討論 / 優雅的拼接反向疊加)
class Solution: def generate(self, numRows): """ :type numRows: int :rtyp
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 II
triangle pan ace arraylist public clas val solution script Description: Given an index k, return the kth row of the Pascal’s triangle. F
leetcode 119. Pascal's Triangle II
[1] note AD gif anim lee media rip turn Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal‘s triangle. N
leetcode: 119. Pascal's Triangle II
Difficulty Easy. Problem Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row
LeetCode演算法題-Pascal's Triangle II(Java實現)
這是悅樂書的第171次更新,第173篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第30題(順位題號是119)。給定非負索引k,其中k≤33,返回Pascal三角形的第k個索引行。行索引從0開始。在Pascal的三角形中,每個數字是它上面兩個數字的總和。例如: 輸