LeetCode & 118-Pascal's Triangle-Easy
Array
Description:
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]
]
这题依旧没写出来,看到要返回的是List<List<Integer>>
直接就蒙了。感想就是,我好菜好菜好菜,一定一定要看Java源码!然后恶补了Arraylist源码,再次感叹Best Solution比我厉害多了。
Best Solution:
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
for (int i = 0; i < numRows; i++) {
// 注意每次在队首加1,其他元素后移
row.add(0, 1);
for (int j = 1; j < i; j++) {
row.set(j, row.get(j) + row.get(j+1));
}
list.add(new ArrayList<Integer>(row));
}
return list;
}
}
刚开始没有理解这个解法的思想,因为对大循环不理解,row.add(index, element)
这一步很关键,把最新的这行数组复制了前一行内容,并在队首加1,其余元素后移,这样在row.set(index, element)
就很自然的将两元素值相加得到新的值。
在此说明在看源码过程中得到的收获:
Arraylist.add(index, element)
public void add(int index, E element) {
//判断索引位置是否正确
rangeCheckForAdd(index);
// 扩容检测
ensureCapacityInternal(size + 1); // Increments modCount!!
// 复制现有数组,后移一位
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
Arraylist.add(element)
public boolean add(E e) {
// 从队尾插入
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
此处主要区分:add(0,1)
和add(1)
的区别,一个在队首插入,一个队尾插入
Arraylist.set(index, element)
public E set(int index, E element) {
rangeCheck(index);
// 在index位置上替代,故先add才能set
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
LeetCode & 118-Pascal's Triangle-Easy的更多相关文章
- 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 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- 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 ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
- [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, ...
随机推荐
- c#开发wps插件(3)部署
上一篇,我们完成了具体的开发工作,但是最终必须得部署到客户机器上.所以,部署方面,我花费了一些时间去研究,现在总结下.上一篇中,我为什么建议开发人员安装wps专业版呢?因为装了专业版,方便我们开发,安 ...
- Spring @AfterReturning 总是返回null
在学习Spring Aop时,遇到一个问题,当 @Around(环绕通知)与 @AfterReturning(后置通知)共存 时,@AfterReturning 通过属性 returning = &q ...
- Firefox取消“订阅实时书签”功能
如果你勾选了Firefox“总是用实时书签订阅收取”, 一打开RSS页面就直接弹出添订阅实时书签对话,而当你想在火狐中直接查看RSS页面时,发现不知如何取消“订阅实时书签”功能了.就怨这个关闭功能藏的 ...
- curl post请求总是返回417错误
在进行post请求的时候, curl总是返回417错误 在使用curl做POST的时候, 当要POST的数据大于1024字节的时候, curl并不会直接就发起POST请求, 而是会分为俩步. 发送一个 ...
- java容器类4:Queue深入解读
Collection的其它两大分支:List和Set在前面已近分析过,这篇来分析一下Queue的底层实现. 前三篇关于Java容器类的文章: java容器类1:Collection,List,Arra ...
- NGUI_Depth
四.深度(Depth)概念; 1. (1).每一个UIPanel和每一个UI控件都一定会有一个Depth,深度值大代表显示的优先级高(会趋向于在界面更上层显示) (2).Depth决定的是UI的显示层 ...
- 求第k小的元素
用快排解决: 用快排,一趟排序后,根据基准值来缩小问题规模.基准值的下角标i 加1 表示了基准值在数组中第几小.如果k<i+1,那就在左半边找:如果k>i+1那就在右半边找.当基准值的下角 ...
- python作业02
1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex', 'eric', 'rain'] li = ['alex', 'eric', 'rain'] v = "_& ...
- C语言第一次博客作业
一,PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- 基于JavaMail向邮箱发送邮件
参考:http://blog.csdn.net/ghsau/article/details/17839983 http://blog.csdn.net/never_cxb/article/detail ...