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, ...
随机推荐
- eclipse 精确查询
---恢复内容开始--- ctrl+H(一般都是这个,如果无效看你的自定义快捷键) 输入\b 查询的字符串 \b 后面的正则表达式选框必须选
- 【经验随笔】Restlet Client发送XML格式参数消息
背景介绍 Restlet Client是一款模拟客户端发送http请求的chrome插件,可以作为驱动调试后台程序,个人觉得比POSTMAN好用.CXF是一款发布restful服务的开源组件. 编写r ...
- 使用netflix Zuul 代理你的微服务
构建 "微服务" 时的一个常见挑战是为系统的使用者提供一个统一的接口.您的服务被分割成一个个积木式的小程序,事实上这些细节本不应该对用户可见. 为了解决这个问题, Netflix ...
- Java 小记 — Spring Boot 的实践与思考
前言 本篇随笔用于记录我在学习 Java 和构建 Spring Boot 项目过程中的一些思考,包含架构.组件和部署方式等.下文仅为概要,待闲时逐一整理为详细文档. 1. 组件 开源社区如火如荼,若在 ...
- UWP 图片模糊
先看一下效果: 这是微识别的个人中心页面,顶部有头像,以及背景图片模糊. 要实现这样的效果,有两种方法. 第一种麻烦点,也是我现在用的.想看简单的,翻到最后 1. 首先看一下xaml代码: <S ...
- java语言与jvm虚拟机简介
一.java语言 1.1 支持面向对象编程oop 强调支持,因为java同样可以面向过程编程. oop的三大特性是:封装.继承.多态. 封装主要针对成员变量而言,oop的思想要求成员变量均为私有,不应 ...
- Android Studio报错:the selected directory is not a valid home for unknow sdk
今天在使用Android Studio的时候不知道怎么了,没有import module,视图里面也没有android视图,查看project设置.提示我的SDK路径无效:the selected d ...
- Bootstrap3 datetimepicker控件的使用
Bootstrap3 日期+时间选择控件 1.支持日期选择,格式设定 2.支持时间选择 3.支持时间段选择控制 4.支持中文 官网地址:http://eonasdan.github.io/bootst ...
- 在用jQuery时遇到的小问题
1. class类名问题? 在我在class ='看看(2)' ,凡是这样的居然给自身加其他style样式,居然添加不上,后来改成其他类名不带括号里的,居然好了. 2. line-height 引入的 ...
- docker环境下使用xdebug进行断点调试
最近把本地环境切换成了docker的环境,便于快速运行和开发,确实比较给力,但是也遇到了问题,以前的本地xdebug断点调试都用不了,弄了几个小时终于搞定了 docker还是坑多,绕,下面把docke ...