首页 > 其他分享 >LeetCode //C - 119. Pascal‘s Triangle II

LeetCode //C - 119. Pascal‘s Triangle II

时间:2024-05-25 20:58:55浏览次数:17  
标签:Triangle rowIndex element II int Pascal each row

119. Pascal’s Triangle II

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:
在这里插入图片描述
 

Example 1:

Input: rowIndex = 3
Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0
Output: [1]

Example 3:

Input: rowIndex = 1
Output: [1,1]

Constraints:
  • 0 <= rowIndex <= 33

From: LeetCode
Link: 119. Pascal’s Triangle II


Solution:

Ideas:

1. Memory Allocation: The function uses malloc to allocate memory for the array that will store the rowIndex-th row. The size of the array is rowIndex + 1.

2. Initialization: The first element of each row is always 1, so row[0] = 1.

3. Calculating Row Elements:

  • The outer loop runs from 1 to rowIndex to construct each row up to the desired rowIndex.
  • Inside the outer loop, the last element of each row is set to 1.
  • The inner loop updates the elements of the row in reverse order (from right to left) to avoid overwriting the values needed for the calculations. Each element row[j] is updated as the sum of row[j] and row[j-1].

4. Returning the Result: The function returns the pointer to the array containing the desired row.

Code:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getRow(int rowIndex, int* returnSize) {
    *returnSize = rowIndex + 1;
    int* row = (int*)malloc((*returnSize) * sizeof(int));
    
    row[0] = 1;  // The first element is always 1

    for (int i = 1; i <= rowIndex; i++) {
        row[i] = 1;  // The last element in each row is always 1
        for (int j = i - 1; j > 0; j--) {
            row[j] = row[j] + row[j - 1];
        }
    }

    return row;
}

标签:Triangle,rowIndex,element,II,int,Pascal,each,row
From: https://blog.csdn.net/navicheung/article/details/139203334

相关文章

  • Day 4 | 24. 两两交换链表中的节点 、 19.删除链表的倒数第N个节点 、面试题 02.07.
    24.两两交换链表中的节点用虚拟头结点,这样会方便很多。本题链表操作就比较复杂了,建议大家先看视频,视频里我讲解了注意事项,为什么需要temp保存临时节点。题目链接/文章讲解/视频讲解:https://programmercarl.com/0024.两两交换链表中的节点.html思考需要设置一个虚拟头节点,方......
  • 【刷题笔记Day2】数组|977.有序数组的平方、209. 长度最小的子数组、59.螺旋矩阵II
    文章目录977.有序数组的平方解题思路遇到的问题及解决方案209.长度最小的子数组解题思路遇到的问题及解决方案59.螺旋矩阵II解题思路遇到的问题及解决方案总结977.有序数组的平方题目描述:给你一个按非递减顺序排序的整数数组nums,返回每个数字的平方组成的新......
  • IIC时序分析
    转载博客:https://www.cnblogs.com/liujinggang/p/9656358.html以及https://www.cnblogs.com/xuyan123/p/14134246.html我也怕什么时候大神把博客删除了,后面这个链接简单明了,一下就让我看明白了iic的基础。一.IIC总线协议介绍:I2C总线是由数据线SDA和时钟SCL构成的串行......
  • aws jsii 基于js 实现跨语言交互的编译器
    jsiiaws开源的,让我们可以基于js实现跨语言交互的编译器,我们可以基于ts开发功能,然后通过编译器jsii可以实现其他语言的通信,目前支持C#,golang,java,pythonruntime参考架构如下图说明从架构上我们可以看出jsii的通信是基于了标准输入输出的处理,实际内部处理后边研究下参考资......
  • 动态规划之打家劫舍I和II
            这次分享leetcode上关于动态规划的两道题,打家劫舍I和打家劫舍II。打家劫舍你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统......
  • 零基础学Java第二十一天之IIO流之对象流
    IO流之对象流1、对象流1、理解将对象写入到文件,将文件里的对象读取到程序中classObjectInputStream–对象输入流classObjectOutputStream–对象输出流序列化/钝化:程序里的对象写入到文件中反序列化/活化:文件中的对象读取到程序中2、注意对象所属的类......
  • python DataFrame之MultiIndex 的使用
    importpandasaspdimportpprintasp#嵌套列表arrays=[['a','a','b','b'],[1,2,1,2]]#创建MultiIndexindex=pd.MultiIndex.from_arrays(arrays,names=('letter','number'))#使用MultiInd......
  • 获取iis中站点连接数
    必须要有注册表的访问权限1System.Threading.Tasks.Task.Run(async()=>2{3while(true)4{5try6{7//获取当前站点连接数8varcounter=newSystem.Diagnostics.PerformanceCounter9{......
  • RTL8211F以太网千兆RGMII开发板-飞录科技
    1.概述    RGMII 开发板主芯片是RTL8211FD。配套国产GOWIN的2AR-18和NR-9C的开发板,测试RGMII的千兆以太网数据发送和接收功能。  开发板的代码是基于MAC模式,通过循环发送计数器来判断包发送和接收是否正确。          配套资料  ......
  • 力扣-1209. 删除字符串中的所有相邻重复项 II
    1.题目题目地址(1209.删除字符串中的所有相邻重复项II-力扣(LeetCode))https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string-ii/题目描述给你一个字符串 s,「k倍重复项删除操作」将会从s 中选择 k 个相邻且相等的字母,并删除它们,使被删去的字符串的......