leetcode Pascal's triangle
#include <stdio.h>
int** generate(int numRows, int** columnSizes) {
if (numRows == 0) {
columnSizes = NULL;
return NULL;
}
int** res = NULL;
res= (int **) malloc (numRows*sizeof(int *));
(*columnSizes) = (int *) malloc(numRows*sizeof(int));
int i,j;
for (i = 0; i< numRows; i++) {
res[i] = (int*) malloc((i+1)*sizeof(int));
(*columnSizes)[i] = i+1;
if (0==i) {
res[i][0] = 1;
}
else {
res[i][0] = 1;
res[i][i] = 1;
if (i > 1) {
for (j =1; j<i;j++) {
res[i][j] = res[i-1][j-1]+ res[i-1][j];
}
}
}
}
return res;
}
int main (int argc, char * argv[]) {
int *Size = NULL;
int **res = generate(5, &Size);
if (Size != NULL && res != NULL) {
int i,j;
for (i =0; i<5; i++) {
for (j = 0; j< Size[i];j++) {
fprintf(stdout,"%d \n",res[i][j]);
}
fprintf(stdout,"\n");
}
for (i = 0; i < 5;i++) {
if (res[i] != NULL) {
free(res[i]);
res[i]=NULL;
}
}
free(res);
res = NULL;
if (Size != NULL) {
free(Size);
Size = NULL;
}
}
return 0;
}
leetcode Pascal's triangle的更多相关文章
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- [leetcode]Pascal's Triangle @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- LeetCode: Pascal's Triangle 解题报告
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
随机推荐
- MVC中view和controller相互传值的方法
MVC项目中,在view层如果使用前台框架,框架中会有封装好的相互传值方法.但是,那些postdata[][]方法不一定能够满足功能需求,反而一些常用的传值方法可能会刚好解决它们的不足.总结如下: 一 ...
- wsdl学习
本文来自 :迹忆 原文地址:http://www.onmpw.com/tm/xwzj/network_47.html 在刚开始学习Webservice的时候,发现里面涉及到的知识点还真不少,每一点单拿 ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- HDU5795A Simple Nim SG定理
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- java(2)之前往对象村
这次说一说面向对象与面向过程的区别以及面向对象的优点.
- Oracle客户端配置
1. 打开开发生产数据库系统,点击下载Oracle_12C_Client32,并且解压缩. 2. 找到文件下的setup.exe文件,并且执行. 3. 等待数秒,在如下界面中选择第二项,管理员, ...
- mysql中更新或者删除语句中子语句不能操作同一个表You can't specify target table 'test' for update in FROM clause
问题描述:有个数据表test,有个字段value,如下 mysql> select * from test;+----+------------------------------------+ ...
- 升级openssl环境至openssl-1.1.0c
升级openssl环境至openssl-1.1.0c1.查看源版本 [root@zj ~]# openssl version -aOpenSSL 1.0.1e-fips 11 Feb 2013 2.下 ...
- 一位程序员如何修炼成CTO
几乎整个互联网行业都缺CTO,特别是一些草根背景的创业者,这个问题更加显著.从我自己的感受,身边各种朋友委托我找CTO的需求,嗯,算下来超过两位数了,光最近一个月就有3个,而且这三家都是刚拿了A轮的. ...
- Linux命令:screen
①杀死detached状态的会话: $ screen -X -S [session # you want to kill] quit