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 == ) return ;
int** Array = (int **)malloc(numRows * sizeof(int *));
*columnSizes = (int *)malloc(numRows * sizeof(int));
for(i = ; i < numRows; i++){
(*columnSizes)[i] = i + ;
Array[i] = (int *)malloc((i + ) * sizeof(int));
for(j = ; j < i + ; j++){
if((j == ) || (j == i))
Array[i][j] = ;
else
Array[i][j] = Array[i - ][j - ] + Array[i - ][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
Leecode刷题之旅-C语言/python-118杨辉三角的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- Service Broker完成实例之间的会话详细解读
首先了解service broker是什么东西: Service Broker 是数据库引擎的组成部分,因此管理这些应用程序就成为数据库日常管理的一部分. Service Broker 为 SQL S ...
- c#中abstract、override、new、virtual、sealed使用和示例
原文地址:http://blog.csdn.net/richerg85/article/details/7407544 abstract 修饰类名为抽象类,修饰方法为抽象方法.如果一个类为抽 ...
- Selenium2学习(四)-- xpath定位
前言 在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. ...
- vue v-on:事件
1.js代码 var box=new Vue({ el:'.box', data:{ msg:'hello' }, methods:{ /*方法放置区,函数*/ show:function(){ // ...
- June 10th 2017 Week 23rd Saturday
A lot of things, we can be touched, but we can not shed tears. 很多事情,我们可以感动,却不能流泪. Sometimes I was to ...
- python创建项目
一.准备下载 python3.6.6 https://www.python.org/downloads/windows/(需要注意你的电脑是32位还是64位) mysql 5.1.72 https:/ ...
- ORACLE常用函数汇总(持续更新中....)
在使用ORACLE过程中,把一些常用的函数的相关用法,注意事项进行简单的汇总,便于自己查询参考. DBMS_RANDOM包 dbms_random是一个可以生成随机数值或者字符串的程序包.这个包有in ...
- 管道(Pipelines)模型
Pipeline模型最早被使用在Unix操作系统中.据称,假设说Unix是计算机文明中最伟大的发明,那么,Unix下的Pipe管道就是尾随Unix所带来的还有一个伟大的发明[1].我觉得管道的出现,所 ...
- BZOJ3262:陌上花开(CDQ分治)
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美 ...
- 【洛谷5283】[十二省联考2019] 异或粽子(可持久化Trie树+堆)
点此看题面 大致题意: 求前\(k\)大的区间异或和之和. 可持久化\(Trie\)树 之前做过一些可持久化\(Trie\)树题,结果说到底还是主席树. 终于,碰到一道真·可持久化\(Trie\)树的 ...