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 ...
随机推荐
- jQuery文件上传插件jQuery Upload File 有上传进度条
jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...
- AngularJS使用指南
ng-app 定义一个AngularJS应用程序 ng-model 把元素值绑定到AngularJS应用程序 ng-blind 把AngularJS应用程序数据绑定到HTML视图上 ng-init 初 ...
- android appwigt
package com.example.test1; import android.os.Bundle; import android.app.Activity; import android.con ...
- boldSystemFontOfSize 和 systemFontOfSize 的区别
使用 UIFont 的下列方法: + systemFontOfSize + boldSystemFontOfSize + italicSystemFontOfSize p.p1 { margin: 0 ...
- 1013. Battle Over Cities
好久都没有做题了,从长沙回来之后一直就是看看QT,感觉自己真的要蠢死了><不开心不开心 题目大概意思就是从一个图里面去掉一个点,看看剩下多少个孤立点. 自己想了好大一会儿没有思路,看到网上 ...
- 不再为Apache进程淤积、耗尽内存而困扰((转))
本篇文章是为使用Apache+MySQL,并为Apache耗尽内存而困扰的系统管理员而写.如果您没有耐心读完本文,请参考以下步骤: 修改/etc/my.cnf,加上这样一行: log-slow-que ...
- 用SignalR实现的共享画板例子
使用HTML5的canvas画布功能,在页面进行绘画,然后通过SignalR将画布的每个点的颜色提交到服务端,服务端同时将这些画布的信息推送到其他客户端,实现共享同一个画板的功能 类似下图,在某一个浏 ...
- Android导航栏菜单强制转换
private void getOverflowMenu() { ViewConfiguration viewConfig = ViewConfiguration.get(this); try { F ...
- .net连接DB2的异常SQL0666 - SQL query exceeds specified time limit or storage limit.错误处理
SQL0666 - SQL query exceeds specified time limit or storage limit. 原因:查询超时 解决办法: set the DbCommand.C ...
- public protect private. 草稿。
public protect private. 草稿. #include <iostream> #include <thread> #include <memory> ...