LeetCode(30)-Pascal's Triangle
题目:
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路
- 题意是要用数组表示帕斯卡三角形
- 输入一个数值,显示相应行数的【帕斯卡三角形
- 根据下一行和上一行的递推公式来处理
- 设置一个变量fisrt来记录上一行的数据,最新一行的是新建的
-
代码
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> all = new ArrayList<List<Integer>>();
List<Integer> first = new ArrayList<Integer>();
if(numRows == 0){
return all;
}
if(numRows == 1){
List<Integer> a1 = new ArrayList<Integer>();
a1.add(1);
all.add(a1);
return all;
}
if(numRows > 1){
List<Integer> a2 = new ArrayList<Integer>();
a2.add(1);
all.add(a2);
}
for(int i = 1;i < numRows;i++){
List<Integer> next = new ArrayList<Integer>();
next.clear();
next.add(1);
for(int a = 1;a < i;a++){
if(a-1 >= 0 )
next.add(first.get(a-1)+first.get(a));
}
next.add(1);
all.add(next);
first.clear();
if(next != null){
for(int o = 0;o < next.size();o++){
first.add(next.get(o));
}
}
}
return all;
}
}
LeetCode(30)-Pascal's Triangle的更多相关文章
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 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 II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- LeetCode 119. 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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
随机推荐
- velocity中加载模板文件的方式
velocity有多中种方式供我们去加载我们自定义的模板文件,下面详细的介绍使用的方法. 1.1.1. 加载classpath目录下的模板文件 使用classpath方式加载,是我们经常用到的一种方式 ...
- TCP的发送系列 — tcp_sendmsg()的实现(二)
主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上篇blog中分析了tcp_sendmsg()这个主要函 ...
- Android缩放动画
Android缩放动画 核心方法 public void startAnimation(Animation animation) 执行动画,参数可以是各种动画的对象,Animation的多态,也可以是 ...
- C++_友元函数
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护 ...
- LAPACK的C/C++接口及代码实例
今天介绍一个矩阵处理工具LAPACK,她有C\C++接口,可在windows下移植.本人最近正在学习,发现还是还不错滴~ 本博文分为三部分,第一部分介绍LAPACK的安装,这里只介绍最简单的部署:第二 ...
- 分布式进阶(四)Ubuntu 14.04下安装Tomcat 6
Ubuntu 10.04下安装Tomcat 6 1,下载apache-tomcat6, 地址链接:http://archive.apache.org/dist/tomcat/tomcat-6/v6.0 ...
- Unity2D Sprite灰白图(Unity3D开发之十一)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=596 昨晚看到群里问到2DSpr ...
- ROS(indigo)机器人操作系统学习资料和常用功能包汇总整理(ubuntu14.04LTS)
ROS(indigo)机器人操作系统学习资料和常用功能包汇总整理(ubuntu14.04LTS) 1. 网站资源: ROSwiki官网:http://wiki.ros.org/cn GitHub ...
- C++对象模型(五):The Semantics of Data Data语义学
本文是<Inside the C++ Object Model>第三章的读书笔记.主要讨论C++ data member的内存布局.这里的data member 包含了class有虚函数时 ...
- byte和长度为8的boolean数组互相转换
由于byte是一个8位字节 所以可以用它来存放数组为8的boolean数组,这些在通信协议会经常用到.这里给出一个java代码对其互相转换的. package com.udpdemo.test2; i ...