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 ...
随机推荐
- PB调用C#编写的DLL
C#以其简单易用,功能强大深受大家喜爱.PowerBuilder作为C/S的MIS开发工具,十分简单灵活,开发时间短,开发及维护成本低,一直是中小企业信息管理系统的首选开发工具.但是PB的局限性限制了 ...
- HttpClient拉取连载小说
上午刚入手的小说,下午心血来潮想从网站上拉取下来做成电子书,呵呵,瞎折腾-说做就做- [抓包] 这一步比什么都重要,如果找不到获取真正资源的那个请求,就什么都不用做了- 先是打算用迅雷把所有页面都下载 ...
- 4.Zabbix 3.0 案例
请查看我的有道云笔记: http://note.youdao.com/noteshare?id=2807c0910cd63d309e1462128a31ae0e&sub=241A94E5717 ...
- February 28 2017 Week 9 Tuesday
Time you enjoy wasting, was not wasted. 你乐于挥霍的时间,都不能算作是浪费. A few days ago, I learned a sentence from ...
- 一点一点学写Makefile(3)-增加第三方库和头文件
我们在写代码的时候不一定都是有自己来完成,一个工程中会大量使用一些比较优秀的动态库.静态库等,我们在使用这些库完成所有的代码后,需要在编译的时候将这些库使用的头文件添加到我们的工程上,将他的库文件也添 ...
- Android(java)学习笔记21:Java异常处理机制
1. try....catch / try...catch...finally package cn.itcast_02; /* * 我们自己如何处理异常呢? * A:try...catch... ...
- luogu P1503 鬼子进村
嘟嘟嘟 线段树好题. 其实挺水的,想暴力怎么做:每一次从这个点开始向两边扩,直到遇到第一个摧毁的房屋. 那么把暴力改成倍增,然后线段树查询区间和是否为0.时间复杂度O(nlog2n). 题解好像有线段 ...
- [USACO15OPEN]haybales Trappe…
嘟嘟嘟 刚开始我以为如果这头牛撞开一个干草堆的话,获得的冲刺距离只有新增的部分,但实际上是加上原来的部分的. 暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度O(n2). 优化想起来也不难 ...
- 推荐一个zookeeper信息查看工具
zookeeper信息查看工具 下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 解压,打 ...
- caffe+opencv3.3.1
跟着时代走 换成opencv3.3.1,目前来看所有的都是最新版了. anaconda最新,opencv最新,我看了protobuf也很新. 下次再买台服务器时,我想直接用python来弄,因为这次安 ...