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 ...
随机推荐
- blog test
try my first blog by cnblog. i will record my learn experence in the future.
- nagios centos7 rpm打包
wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.3.1/nagios-4.3.1.tar ...
- 深入JDK源码,这里总有你不知道的知识点!
Java的基础知识有很多,但是我认为最基础的知识应该要属jdk的基础代码,jdk的基础代码里面,有分了很多基础模块,其中又属jdk包下面的lang包最为基础. 我们下面将总结和分析一下lang包下面最 ...
- SpringMvc-自定义视图
1.创建视图: 注意:创建视图的时候需要实现View接口的俩个方法 package com.atguigu.springmvc.views; import java.util.Date; import ...
- pl/sql中的一种链接数据库方式
今天看开发人员如此连接数据库:
- C/C++中构造函数和析构函数能否被继承
http://bbs.csdn.net/topics/390160673 标准方面做了要求的.Even though destructors are not inherited 构造函数和析构函数是不 ...
- 在Hibernate单向一对多关联关系中的org.hibernate.StaleStateException 异常。
具体异常如下: Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count fro ...
- codeforces 676C
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- springboot(服务端接口)获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 一.下面为7种服务端获取前端传过来的参数的方法 常用的方法为:@RequestParam和@Req ...
- c#类的练习
类部分练习题 - dijiaxing1234的博客 - CSDN博客 https://blog.csdn.net/dijiaxing1234/article/details/81230811 真好啊