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, ...
随机推荐
- 智能合约语言 Solidity 教程系列8 - Solidity API
这是Solidity教程系列文章第8篇介绍Solidity API,它们主要表现为内置的特殊的变量及函数,存在于全局命名空间里. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应 ...
- 进入PE后不显示硬盘的解决办法
其实我很早之前就知道这个方法了,我虽然不知道原因,不过我是一个一个试出来的,转过来备忘, 内容介绍:经常使用PE的朋友相信都遇到过这样的问题,一些新购买的电脑可以正常把PE系统安装到U盘中,也可以正常 ...
- 关于Android sdkmanager目录结构的总结
SDK Platform是指一些已经编写好的库函数,类文件,我们可以直接调用 Samples for SDK是指一些样本代码,可以导入eclipse运行出来查看里面函数的效果 以system imag ...
- windows 设置/修改全局快捷键
打开控制面板,小图表显示下 点击 管理工具项, 将自己想要谁知快捷键的程序的快捷方式放进去,(需要确认管理员权限) 如图,第一个即为 lz添加的 右击选择属性 在快捷键处同时按下你想要的组合键即可(不 ...
- 笔记:MyBatis XML配置详解
MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 properties ...
- protobuf(quickStart)
1.简介 Protocol Buffers是Google开发一种数据描述语言,能够将数据进行序列化,可用于数据存储.通信协议等方面. 可以理解成更快.更简单.更小的JSON或者XML,区别在于Prot ...
- Android Service 基础
启动方式 startService(Intent) 这种方式启动的Service可以在后台无限期的运行,与启动它的组件没有关系. bindService 绑定Service.它提供了一种类似C/S结构 ...
- js获取input file文件二进制码
<html> <body> <img id="image"src=""/> <br/> <input ty ...
- c++ --> union介绍
union介绍 共用体,也叫联合体,在一个“联合”内可以定义多种不同的数据类型, 一个被说明为该“联合”类型的变量中,允许装入该“联合”所定义的任何一种数据,这些数据共享同一段内存,以达到节省空间的目 ...
- SQL Quick Reference From W3Schools
SQL Statement Syntax AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition AL ...