[leetcode]118,119PascalsTriangle,杨辉三角1,2
杨辉三角1
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]
]
构建杨辉三角,从第一行开始构建,比较简单
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
if (numRows < 1)
return res;
List<Integer> one = new ArrayList<>();
one.add(1);
res.add(one);
for (int i = 1; i < numRows; i++) {
List<Integer> cur = new ArrayList<>();
cur.add(1);
int num = 1;
while (num < i)
{
cur.add(res.get(i-1).get(num)+res.get(i-1).get(num-1));
num++;
}
cur.add(1);
res.add(cur);
}
return res;
}
杨辉三角2
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
要求直接输出第K行
由于有空间限制,只能在本地进行构建杨辉三角,内循环用来更新数据,外循环用来记录第几行
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>();
if(rowIndex<0)
return res;
//第一行
res.add(1);
for(int i=1;i<=rowIndex;i++)
{
//从后边开始向前更新数据,第一个数不更新
for(int j=res.size()-2;j>=0;j--)
{
//当前的数加上前边的数就是下一行的当前位置数
res.set(j+1,res.get(j)+res.get(j+1));
}
//循环完后加上最后的1就是更新好了一行
res.add(1);
}
return res;
}
[leetcode]118,119PascalsTriangle,杨辉三角1,2的更多相关文章
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
- 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- 【easy】118.119.杨辉三角
这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- LeetCode:杨辉三角【118】
LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
随机推荐
- 极简Linux下安装极简桌面
sudo apt install -y xorg lxde-core vnc4server 设置密码:vncpasswd 然后先开启服务,然后再终止服务:(这是为了创建一个默认的配置文件)vncser ...
- 关于java链接装载的思考
遇到个bug,noClassFoundEx,很常见. 但是问题来了. 比如我的服务器目录是 /opt/tomcat/webapps/ROOT/WEB-INF/classes/cn/controller ...
- 转1:Python字符编码详解
Python27字符编码详解 声明 一 字符编码基础 1 抽象字符清单ACR 2 已编码字符集CCS 3 字符编码格式CEF 31 ASCII初创 311 ASCII 312 EASCII 32 MB ...
- PyQt(Python+Qt)学习随笔:信号签名中的万能Python类型PyQt_PyObject
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 信号在定义时参数如果使用字符串'PyQt_PyObject'表示参数可以是任何Python类型,这通 ...
- Mac下安装Mesa
下载Mesa源代码: git clone https://gitlab.freedesktop.org/mesa/mesa.git 如果下载太慢,请参看 下载国外资源. 我下载后看到的Mesa版本信息 ...
- 前端性能测试(H5性能测试)
前端性能调优方法同样适用于H5. 1.H5前端性能知识点 学习前端性能,必须对HTTP协议有所了解. 1.1 浏览器渲染引擎 浏览器是Html解析和页面最终展示的工具. 浏览器的主要功能:将用户选择的 ...
- LeetCode初级算法之数组:1 两数之和
两数之和 题目地址:https://leetcode-cn.com/problems/two-sum/ 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整 ...
- CSS基础-层叠与继承
继承 一些属性可以被继承,一些不可以. 一般来说,字体颜色.字体大小会被继承,关于形状的如 padding .border .margin.width等就不会被继承.哪些属性属于默认继承很大程度上是由 ...
- 【题解】P1852 跳跳棋
link 题意 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.棋盘上有3颗棋子,分别在 \(a,b,c\) 这三个位置.我们要通过最少的跳动把他们的位置移动成 \(x,y, ...
- 【题解】「UVA1149」装箱 Bin Packing
做法显然:贪心,但是怎么贪? 首先从大到小或从小到大排序,窝这次是从大到小排,这样更容易理解(从小到大更方变) 然后设置两个变量 front 和 after 作为前指针和后指针. 循环判断: 当前后两 ...