Pascal's Triangle & II 解题报告

杨辉三角,分别求前n行和第n行。

【求杨辉三角前n行】

Given numRows, generate the first numRows of Pascal‘s triangle.

For example, given numRows = 5,

Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

基础题,直接看代码,注意边界。

public class Solution {
    public List<List<Integer>> generate1(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();

        if (numRows == 0) return ret;

        List<Integer> list0 = new ArrayList<Integer>();
        list0.add(1);
        ret.add(list0);

        for (int i = 2; i <= numRows; i++) {
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);

            List<Integer> pre = ret.get(ret.size()-1);
            for (int j = 1; j < i-1; j++) {
                list.add(pre.get(j-1) + pre.get(j));
            }

            list.add(1);
            ret.add(list);
        }

        return ret;
    }

    // another style
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();

        for (int i = 0; i < numRows; i++) {
            List<Integer> list = new ArrayList<Integer>(); 

            if (i == 0) {
                list.add(1);
            } else {
                List<Integer> pre = ret.get(ret.size()-1);
                for (int j = 0; j <= i; j++) {
                    if (j == 0 || j == i) {
                        list.add(1);
                    } else {
                        list.add(pre.get(j-1) + pre.get(j));
                    }
                }
            }

            ret.add(list);
        }

        return ret;
    }
}

【求杨辉三角第n行】

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?

注意 [1] 算第0行,[1, 1] 为第1行……

用O(k)的空间记录前一行就行了。

public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);

        List<Integer> pre = new ArrayList<Integer>(list);
        for (int i = 1; i <= rowIndex; i++) {
        	list.clear();
            list.add(1);
        	for (int j = 1; j < pre.size(); j++) {
                list.add(pre.get(j) + pre.get(j-1));
            }
            list.add(1);
            pre.clear();
            pre.addAll(list);
        }

        return list;
    }
}

Pascal's Triangle & II 解题报告

时间: 2024-08-02 23:23:35

Pascal's Triangle & II 解题报告的相关文章

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven 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 us

Java [Leetcode 119]Pascal&#39;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]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

(LeetCode)Pascal&#39;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? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

Java for LeetCode 118 Pascal&#39;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]. 解题思路: 注意,本题的k相当于上题的k+1,其他照搬即可,JAVA实现如下: public List<Integer> getRow(int rowIndex) { List<Integer> alist=new ArrayList<Integer>();

LeetCode:Pascal‘s Triangle II - 帕斯卡三角形2

1.题目名称 Pascal's Triangle II(帕斯卡三角形2) 2.题目地址 https://leetcode.com/problems/pascals-triangle-ii/ 3.题目内容 英文:Given an index k, return the kth row of the Pascal's triangle. 中文:给出行数k,返回帕斯卡三角形的第k行 例如,k=3时,返回[1,3,3,1] 4.解题方法1 帕斯卡三角形也叫杨辉三角形,在LeetCode第118题(Pas

119. Pascal&#39;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? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

Pascal&#39;s Triangle II 解答

Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Solution Similar with Pascal's Triangle. Note that the index starts from 0. 1 public class Solution { 2 public List<Integer> getRow(in

[leedcode 119] Pascal&#39;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]. public class Solution { public List<Integer> getRow(int rowIndex) { //由于需要O(k)空间,因此需要借助两个数组保存中间值,并交换两个数组,注意交换的方法! List<Integer> list=new

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&