leetcode Day7----array.easy

Pascal’s Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
在这里插入图片描述

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

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

C语言

/**
 * Return an array of arrays.
 * The sizes of the arrays are returned as *columnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generate(int numRows, int** columnSizes) {
    int **res=(int **)malloc(sizeof(int *)*numRows);
    *columnSizes=(int *)malloc(sizeof(int)*numRows);
    int i,j;
    for(i=0;i<numRows;i++)
    {
        (*columnSizes)[i]=i+1;
        res[i]=(int *)malloc(sizeof(int)*(i+1));
        res[i][0]=1;
        res[i][i]=1;
        for(j=1;j<i;j++)
            res[i][j]=res[i-1][j]+res[i-1][j-1];
    }
    return res;
}

Success
Details
Runtime: 4 ms, faster than 91.86% of C online submissions for Pascal’s Triangle.
Memory Usage: 7.2 MB, less than 8.00% of C online submissions for Pascal’s Triangle.
Next challenges:

python3中有个生成器yeild,yield是一个类似return的关键字,只不过,带有yield的函数,不再是一个普通的函数,而是一个生成器。在执行中,调用next()方法才开始真正执行

python3

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res = [[1]]
        for i in range(1,numRows):
            print(res[-1]+[0],[0]+res[-1])
            res.append(list(map(lambda x, y : x + y, res[-1]+[0], [0]+res[-1])))
        return res[:numRows]

Success
Details
Runtime: 36 ms, faster than 77.44% of Python3 online submissions for Pascal’s Triangle.
Memory Usage: 13 MB, less than 5.83% of Python3 online submissions for Pascal’s Triangle.

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 triangle, each number is the sum of the two numbers directly above it.

Example:

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

Could you optimize your algorithm to use only O(k) extra space?

C语言

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getRow(int rowIndex, int* returnSize) {
    if (rowIndex < 0)
        return NULL;
    
    int *res = (int*)malloc(sizeof(int) * (rowIndex + 1));
    for (int row = 0; row <= rowIndex; row++)
        for (int col = row; col >= 0; col--)
            res[col] = (col == 0 || col == row) ? 1 : res[col] + res[col - 1];
    *returnSize = rowIndex + 1;
    return res;
}

Success
Details
Runtime: 4 ms, faster than 90.82% of C online submissions for Pascal’s Triangle II.
Memory Usage: 6.7 MB, less than 95.00% of C online submissions for Pascal’s Triangle II.

python3

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        row = [1]
        for i in range(1, rowIndex + 1):
            row = [1] + [ row[x] + row[x - 1] for x in range(1, i)] + [1]
        return row

Success
Details
Runtime: 40 ms, faster than 35.55% of Python3 online submissions for Pascal’s Triangle II.
Memory Usage: 13.2 MB, less than 5.12% of Python3 online submissions for Pascal’s Triangle II.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值