LeetCode OJ: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?

思路:

因为每一层的数都是对称的,所以只需要计算每一层的前半部分的值,后半部拷贝就可以了。res[i] = res`[i - 1] + res`[i](res`代表原数组,res代表更新后的数组),tmp代表res`[i - 1],tmp2代表 res`[i]。

代码:

int* getRow(int rowIndex, int* returnSize) {
    if(rowIndex < 0) return NULL;
    ++rowIndex;
    int *res = (int*)malloc(sizeof(int) * rowIndex);
    res[0] = 1;
    int count = rowIndex;
    while(--count){
        int real_index = rowIndex - count + 1;
        int half = real_index / 2;
        if(real_index % 2 == 0){
            half--;
        }
        int tmp = res[0];
        int i = 1;
        for(; i <= half; i++){
            int tmp2 = res[i];
            res[i] = tmp + tmp2;
            tmp = tmp2;
        }
        if(real_index % 2){
            i -= 2;
        } else {
            --i;
        }
        for(int j = half + 1; j < real_index; j++){
            res[j] = res[i--];
        }
    }
    *returnSize = rowIndex;
    return res;
}

LeetCode OJ:Pascal's Triangle II

时间: 2024-10-06 05:26:48

LeetCode OJ:Pascal's Triangle II的相关文章

LeetCode OJ 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? [思路] 我们为了满足空间复杂度的要求,我们新建两个ArrayList,一个负责存储上一个Pascal行的结果,一个根据上一个Pascal行得出当前P

LeetCode OJ:Pascal&#39;s Triangle(帕斯卡三角)

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] ] 帕斯卡三角,很简单的问题,见代码: 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) {

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]. 题目要求计算杨辉三角某一行的元素,这个也是二项式系数的计算问题. class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; vector<int> tmp;

LeetCode OJ:Pascal&#39;s TriangleII(帕斯卡三角II)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 还是帕斯卡三角,只不过这里指定的是某一个特定的层,然后直接返回,这个就可以使用从后往前更新数组的方法,其实I也可以用这个方法来做的,只不过当时没想到啊,代码如下: 1 class Solution { 2 public: 3 vector<int> getRow(int rowInde

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: 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,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

【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]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

leetcode || 119、Pascal&#39;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: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

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

LeetCode OJ:Reverse Linked List II(反转链表II)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt