LeetCode 119 Pascal's Triangle II(帕斯卡三角形II)(vector数学公式)(*)

Posted nomasp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 119 Pascal's Triangle II(帕斯卡三角形II)(vector数学公式)(*)相关的知识,希望对你有一定的参考价值。

翻译

给定一个索引K,返回帕斯卡三角形的第K行。

例如,给定K=3,
返回[1,3,3,1]。

注释:
你可以改进你的算法只用O(k)的额外空间吗?

原文

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?

分析

这一题呢实际上是承接上一题的,我也正好刚写完。

LeetCode 118 Pascal’s Triangle(帕斯卡三角形)(vector)

上一题是返回完整的帕斯卡三角形:

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> pascal;
        if (numRows < 1) return pascal;
        vector<int> root;
        root.push_back(1);
        pascal.push_back(root);
        if (numRows == 1)   return pascal;
        root.push_back(1);
        pascal.push_back(root);
        if (numRows == 2)   return pascal;
        if (numRows > 2) {
            for (int i = 2; i < numRows; ++i) {
                vector<int> temp;
                temp.push_back(1);
                for (int j = 1; j < pascal[i - 1].size(); ++j) {
                    temp.push_back(pascal[i - 1][j - 1] + pascal[i - 1][j]);
                }
                temp.push_back(1);
                pascal.push_back(temp);
            }
            return pascal;
        }
    }
};

所以我就偷个懒,既然是索引K,那就返回它好了,不过效率就……

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

更高效的方法应该是有某种公式了,看看别人写的就知道了……

vector<int> getRow(int rowIndex) {
    vector<int> r;
    r.resize(rowIndex + 1);
    r[0] = r[rowIndex] = 1;
    for (auto i = 1; i < (r.size() + 1) / 2; ++i) {
        r[i] = r[rowIndex - i] = (unsigned long)r[i - 1] * (unsigned long)(rowIndex - i + 1) / i;
    }
    return r;
}

果然,数学的威力再次显现了。让我用Markdown语法写出这个公式……

ri=ri1(indexi+1)/i

Java, updated at 2016/8/28

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

以上是关于LeetCode 119 Pascal's Triangle II(帕斯卡三角形II)(vector数学公式)(*)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 119. Pascal's Triangle II

LeetCode 119. Pascal's Triangle II

C#解leetcode:119. Pascal's Triangle II

Java [Leetcode 119]Pascal's Triangle II

#Leetcode# 119. Pascal's Triangle II

LeetCode 119 Pascal's Triangle II