Leecode刷题之旅-C语言/python-118杨辉三角

/*
 * @lc app=leetcode.cn id=118 lang=c
 *
 * [118] 杨辉三角
 *
 * https://leetcode-cn.com/problems/pascals-triangle/description/
 *
 * algorithms
 * Easy (60.22%)
 * Total Accepted:    17.6K
 * Total Submissions: 29.2K
 * Testcase Example:  ‘5‘
 *
 * 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
 *
 *
 *
 * 在杨辉三角中,每个数是它左上方和右上方的数的和。
 *
 * 示例:
 *
 * 输入: 5
 * 输出:
 * [
 * ?    [1],
 * ?   [1,1],                                    0
 * ?  [1,2,1],
 * ? [1,3,3,1],
 * ?[1,4,6,4,1]
 * ]
 *
 */
/**
 * 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 i,j;
    if(numRows == 0) return 0;
    int** Array = (int **)malloc(numRows * sizeof(int *));
    *columnSizes = (int *)malloc(numRows * sizeof(int));
    for(i = 0; i < numRows; i++){
        (*columnSizes)[i] = i + 1;
         Array[i] = (int *)malloc((i + 1) * sizeof(int));
        for(j = 0; j < i + 1; j++){
          if((j == 0) || (j == i))
          Array[i][j] = 1;
           else
           Array[i][j] = Array[i - 1][j - 1] + Array[i - 1][j];
        }
     }
 return Array;
}

算法核心是很好理解的。如果是首位或者末位,就等于一。否则的话,等于上一轮中两数之和。 如果当前是a[i][j] 那么就等于 a[i-1][j]+a[i-1][j+1]

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=118 lang=python3
#
# [118] 杨辉三角
#
# https://leetcode-cn.com/problems/pascals-triangle/description/
#
# algorithms
# Easy (60.22%)
# Total Accepted:    17.6K
# Total Submissions: 29.2K
# Testcase Example:  ‘5‘
#
# 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
#
#
#
# 在杨辉三角中,每个数是它左上方和右上方的数的和。
#
# 示例:
#
# 输入: 5
# 输出:
# [
# ?    [1],
# ?   [1,1],
# ?  [1,2,1],
# ? [1,3,3,1],
# ?[1,4,6,4,1]
# ]
#
#
class Solution:
    def generate(self, numRows):
        result = []
        if numRows == 0: return []
        for i in range(numRows):
            temp = []
            for j in range(i + 1):
                if j == 0:
                    temp.append(1)
                elif j == i:
                    temp.append(1)
                else:
                    add = result[i - 1][j - 1] + result[i - 1][j]
                    temp.append(add)
            result.append(temp)
        return result

原文地址:https://www.cnblogs.com/lixiaoyao123/p/10529491.html

时间: 2024-12-09 05:47:02

Leecode刷题之旅-C语言/python-118杨辉三角的相关文章

Leecode刷题之旅-C语言/python-28.实现strstr()

/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/implement-strstr/description/ * * algorithms * Easy (37.86%) * Total Accepted: 38.6K * Total Submissions: 102K * Testcase Example: '"hello"\n"ll&

Leecode刷题之旅-C语言/python-26.删除数组中的重复项

/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/ * * algorithms * Easy (42.77%) * Total Accepted: 89.1K * Total Submissions: 208.1K * Testcase Example: '[

Leecode刷题之旅-C语言/python-26.移除元素

/* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-element/description/ * * algorithms * Easy (53.46%) * Total Accepted: 39.5K * Total Submissions: 73.7K * Testcase Example: '[3,2,2,3]\n3' * * 给定一个数组 nums 

Leecode刷题之旅-C语言/python-100相同的树

/* * @lc app=leetcode.cn id=100 lang=c * * [100] 相同的树 * * https://leetcode-cn.com/problems/same-tree/description/ * * algorithms * Easy (51.47%) * Total Accepted: 16K * Total Submissions: 31K * Testcase Example: '[1,2,3]\n[1,2,3]' * * 给定两个二叉树,编写一个函数来

Leecode刷题之旅-C语言/python-121买卖股票的最佳时机

/* * @lc app=leetcode.cn id=121 lang=c * * [121] 买卖股票的最佳时机 * * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ * * algorithms * Easy (48.50%) * Total Accepted: 32.5K * Total Submissions: 66.9K * Testcase Example: '[7,1,5

Leecode刷题之旅-C语言/python-141环形链表

/* * @lc app=leetcode.cn id=141 lang=c * * [141] 环形链表 * * https://leetcode-cn.com/problems/linked-list-cycle/description/ * * algorithms * Easy (35.53%) * Total Accepted: 28.4K * Total Submissions: 79.4K * Testcase Example: '[3,2,0,-4]\n1' * * 给定一个链表

Leecode刷题之旅-C语言/python-349两个数组的交集

/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ * * algorithms * Easy (60.49%) * Total Accepted: 15.1K * Total Submissions: 25K * Testcase Example: '[1,2,2,1]\n[2,2

Leecode刷题之旅-C语言/python-434 字符串中的单词数

/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/ * * algorithms * Easy (29.13%) * Total Accepted: 4.2K * Total Submissions: 14.2K * Testcase Example: '"Hello, m

C语言打印出 杨辉三角

第三题: #include <stdio.h> #include<stdlib.h> int main() //打印 杨辉三角 { int arr[40][40], t = 0, i = 0, j = 0;  // t:高度 int temp = 0;  //temp就是t,表示高度 printf("请输入要打印的杨辉三角的高度(40以内):"); scanf("%d", &t); for (i = 0; i < t; i++)