LeetCode //C - 118. Pascal‘s Triangle

118. Pascal’s Triangle

Given an integer numRows, return the first numRows of Pascal’s triangle.

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

Example 1:

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

Example 2:

Input: numRows = 1
Output: [[1]]

Constraints:
  • 1 <= numRows <= 30

From: LeetCode
Link: 118. Pascal’s Triangle


Solution:

Ideas:

1. Memory Allocation for the Triangle:
The function starts by allocating memory for an array of integer pointers, triangle, which will store the rows of Pascal’s Triangle. The size of this array is numRows, each element of which will be a pointer to an array representing a row in the triangle.

2. Memory Allocation for Column Sizes:
It also allocates memory for an array returnColumnSizes that holds the size of each row. This is needed because each row of Pascal’s Triangle has a different number of elements. For row i, there will be i + 1 elements.

3. Filling the Triangle:
The function then iterates over each row with two nested loops:

  • The outer loop runs from 0 to numRows - 1, setting up each row.
  • The inner loop runs through each row and calculates the value of each element based on the values from the previous row. The first and last elements of each row are always 1.

4. First and Last Elements:
Within each row, before entering the inner loop, the first element is set to 1. After the inner loop, the last element is also set to 1.

5. Summation Rule:
The key operation occurs inside the inner loop, where each element is the sum of the two elements directly above it from the previous row: triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]. This is the summation rule that defines Pascal’s Triangle.

6. Return Values:
Finally, the function sets the returnSize to the number of rows (numRows) to indicate how many rows are in the returned triangle and returns the pointer to the array of arrays, which is Pascal’s Triangle.

Code:
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generate(int numRows, int* returnSize, int** returnColumnSizes) {
    // Allocate memory for the array of arrays
    int** triangle = (int**)malloc(numRows * sizeof(int*));
    *returnColumnSizes = (int*)malloc(numRows * sizeof(int));

    for (int i = 0; i < numRows; i++) {
        // Set the return size for each row
        (*returnColumnSizes)[i] = i + 1;

        // Allocate memory for each row
        triangle[i] = (int*)malloc((*returnColumnSizes)[i] * sizeof(int));
        triangle[i][0] = 1; // first element is always 1

        // Set the elements of the triangle
        for (int j = 1; j < i; j++) {
            // Each element equals the sum of the elements above it
            triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
        }

        triangle[i][i] = 1; // last element is always 1
    }

    // Set the return size
    *returnSize = numRows;
    
    return triangle;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/420995.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spring Cloud 构建面向企业的大型分布式微服务快速开发框架+技术栈介绍

分布式架构图 Cloud架构清单 Commonservice&#xff08;通用服务&#xff09; 1&#xff09;清单列表 2&#xff09;代码结构 Component&#xff08;通用组件&#xff09; 1&#xff09;清单列表 2&#xff09;代码结构 快速开发管理平台——云架构【系统管理平台】 一…

python requests接口自动化测试 (数据库断言)

前言 熟练掌握接口自动化测试体系背后的这些技能和处理问题的思路&#xff0c;实现时间、人力、收益的平衡&#xff0c;对于一个经验尚浅的初、中级测试开发人员来说绝对是一个艰巨的挑战。 五步教会你写接口自动化用例 需要安装三方包:requests pytest pytest-htmlpip insta…

视觉三维重建colmap框架的现状与未来

近两年AI技术的火热尤其是nerf和gaussian splatting的出现&#xff0c;又将colmap推了一把&#xff0c;传统mvs的地位仿佛受到了挑战&#xff0c;虽然说nerf/gs的效果是无法胜任传统mvs的精度&#xff0c;但是作为"看看"的条件&#xff0c;是远远足够了。且传统重复纹…

chartjs 饼状图

之前要把canvas先清除掉&#xff0c;不然刷新数据&#xff0c;还会有前面的图表 function clearCanvas(){$(#donutChart).remove();$(#chartdiv).append(<canvas id"donutChart" style"min-height: 500px; height: 500px; max-height: 500px; max-width: 70%…

一文搞懂 Transformer 工作原理 !!

文章目录 前言 一、单头Attention工作原理 二、多头Attention工作原理 三、全连接网络工作原理 前言 本文将从单头Attention工作原理、多头Attention工作原理、全连接网络工作原理三个方面&#xff0c;实现一文搞懂Transformer的工作原理。 Transformer工作原理 一、单头Atte…

【学习记录】Resnet

Resnet的残差块 BasicBlock模块&#xff1a; Resnet的作用 解决梯度消失。网络越深&#xff0c;会导致梯度消失。Resnet可以解决梯度消失的问题。 Resnet的原理 参考视频&#xff1a;https://www.bilibili.com/video/BV1cM4y117ob/?spm_id_from333.337.search-card.all.cl…

达梦数据库查询语句内存溢出问题解决

背景&#xff1a;达梦数据库使用过程中&#xff0c;某天突然服务宕机&#xff0c;导致各类后端服务无法注册到nacos上&#xff0c;重启之后nacos正常启动&#xff0c;可执行一条两千多条数据量的连表查询时间很长&#xff0c;甚至会报错&#xff0c;经查看日志发现在查询过程中…

恒创科技:服务器CPU核心和线程如何理解?

​  关于 CPU 核心和线程&#xff0c;是服务器处理能力的核心和灵魂&#xff0c;它们决定了服务器执行任务和同时处理多个操作的效率。 那么&#xff0c;服务器中的 CPU 核心和线程到底是什么?如何理解呢? 什么是CPU核心? CPU核心作为CPU(中央处理单元)的主要处理单元。该…

Windows下卸载JDK

操作步骤&#xff1a; 直接到windows程序卸载面板进行卸载 然后删除已配置的环境变量

Python调用ChatGPT API使用国内中转key 修改接口教程

大家好&#xff0c;我是淘小白~ 有的客户使用4.0的apikey ,直接使用官方直连的apikey消费很高&#xff0c;有一位客户一个月要消费2万&#xff0c;想使用4.0中转的apikey&#xff0c;使用中转的apikey 需要修改官方的openai库&#xff0c;下面具体说下。 1、首先确保安装的op…

在vue2中使用饼状图

1.引入vue2和echarts <script src"https://cdn.jsdelivr.net/npm/vue2.7.14/dist/vue.js"></script> <script src"https://cdn.jsdelivr.net/npm/echarts5.4.0/dist/echarts.min.js"></script> 2.1 补充基本的body内容 <div id…

vscode起本地服务

下载这个 插件 Live Server (Five Server) 下载完会出现这个

政安晨【示例演绎虚拟世界开发】(二):Cocos Creator 配置工作环境并运行脚本

在这篇文章中&#xff0c;我们将会为Cocos Creator配置默认的脚本编辑器与预览浏览器&#xff0c;并在配置好的编辑器中实施Cocos Creator脚本编程工作。通过这篇文章&#xff0c;您将会掌握基础的脚本开发知识&#xff0c;同时会对Cocos Creator脚本编程有初步的认知。 配置外…

如何将一台电脑主机分裂成两台、三台?

有用户问&#xff1a;如何将一台电脑主机拆分成两台、三台甚至更多台使用&#xff1f; 这是什么意思&#xff1f; 简单解释一下&#xff1a;在一台计算机主机上&#xff0c;连接两台、三台或者更多台显示器&#xff0c;然后将这台主机的硬件资源分配给这些显示器&#xff0c;然…

退休开便利店真的靠谱吗?2024比较赚钱的创业项目排行

近日多个退休后开便利店赚钱的新闻登上热搜&#xff0c;但是&#xff0c;小编对此有疑问&#xff0c;退休的老年人开便利店真的是一个好选择吗&#xff1f; 第一、便利店最基本的转让费&#xff0c;装修费&#xff0c;进货等等&#xff0c;这笔开支非常大&#xff0c;足以掏空老…

librtmp源码分析

阅读了librtmp的源码&#xff0c;简单记录下。 首先补充下AMF格式基本知识 1 AMF格式 AMF是Action Message Format(动作消息格式)的简写&#xff0c;它是一种二进制的数据格式。它的设计是为了把actionscript里面的数据(包括Object, Array, Boolean, Number等)序列化成二进制…

Spring - InitializingBean、@PostConstruct、@Bean(initMethod = “init“)和构造方法执行优先级比较

执行顺序优先级 构造方法 > postConstruct > afterPropertiesSet > init方法 代码案例 Component public class InitializingBeanTest implements InitializingBean {public InitializingBeanTest(){System.out.println("构造方法");}Overridepublic void…

如何学习、上手点云算法(一):点云基础

写在前面 本文内容 点云算法的学习基础&#xff0c;入门方法&#xff0c;相关领域&#xff0c;资源&#xff0c;开源库&#xff0c;算法等的介绍&#xff1b; 以Open3D和PCL等为基础工具的点云处理代码讲解、实现&#xff1b; 文中涉及的参考以链接形式给出&#xff0c;涉及文…

计算机网络_2.1 物理层概述

2.1 物理层概述 一、物理层要实现的功能二、物理层接口特性 B站 深入浅出计算机网络 2.1物理层概述 一、物理层要实现的功能 物理层要实现的功能就是在各种传输媒体上传输比特0和1&#xff0c;进而给上面的数据链路层提供透明传输比特流的服务。 数据链路层“看不见”&#xff…

Golang 开发实战day01 - Variable String Numeric

Golang 教程01 - Variable String Numeric 1. Go语言的重要性 Go语言&#xff0c;又称Golang&#xff0c;是一种由Google开发的静态编译型编程语言。它于2009年首次发布&#xff0c;并在短短几年内迅速流行起来。Go语言具有以下特点&#xff1a; 语法简单易学&#xff1a;Go…