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的更多相关文章

  1. 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- ...

  2. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  3. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. leetcode 118 Pascal's Triangle ----- java

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  7. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  8. Java for LeetCode 118 Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  9. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

  10. [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, ...

随机推荐

  1. uwsgi服务启动(start)停止(stop)重新装载(reload)

    1. 添加uwsgi相关文件 在之前的文章跟讲到过centos中搭建nginx+uwsgi+flask运行环境,本节就基于那一次的配置进行说明. 在www中创建uwsgi文件夹,用来存放uwsgi相关 ...

  2. 【python学习笔记】9.魔法方法、属性和迭代器

    [python学习笔记]9.魔法方法.属性和迭代器 魔法方法:xx, 收尾各有两个下划线的方法 __init__(self): 构造方法,创建对象时候自动执行,可以为其增加参数, 父类构造方法不会被自 ...

  3. 【python学习笔记】6.抽象

    [python学习笔记]6.抽象 创建函数: 使用def语句定义函数,不用声明参数类型,和返回值类型 def function_name(param1, param2): 'this is docum ...

  4. python selenium 自动化测试web

    如何使用python完成自动化测试web页面呢?首选selenium   那基于python的selenium如何使用,下面看一段测试案例: 基于python的selenium 安装方法: pip i ...

  5. 前端div层级控制

    z-index:20000;使用此属性可以控制div的层级即哪个在上哪个在下

  6. Ext简单demo示例

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. NGUI_Texture

    六.UITexture:在屏幕上显示一张图片,和Sprite类似,但是UITexture会单独消耗一个DrawCall去渲染, 会单独加载进内存,会增加内存的开销. 1.使用UITexture时要遵循 ...

  8. SpringtMVC中配置 <mvc:annotation-driven/> 与 <mvc:default-servlet-handler/> 源码解析

    上一篇有提到,当有.无这两个标签时,SpringtMVC 底层所采用的  HandlerMapping 以及 HandlerAdapter 是不一样的.现在就来进行源码调试,揭开 SpringtMVC ...

  9. Java读取properties文件连接数据库

    先说为什么要有这种东西,或者我们为什么要用这种方式来写,先看经常用的方法,我们经常写的 package util; import java.sql.Connection; import java.sq ...

  10. 笔记:Spring Cloud Ribbon 客户端负载均衡

    Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服 ...