package y2019.Algorithm.array;

import java.util.ArrayList;
import java.util.List; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: GetRow
* @Author: xiaof
* @Description: 119. Pascal's Triangle II
* Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
* Note that the row index starts from 0.
* <p>
* Input: 3
* Output: [1,3,3,1]
* <p>
* 这里和之前的类似,就是获取这个塔的第几行,那么我们每次字需要存上一行,就可以求出下一行数据了
* a[i,j] = a[i - 1][j-1] + a[i - 1][j]
* @Date: 2019/7/2 9:13
* @Version: 1.0
*/
public class GetRow { public List<Integer> solution(int rowIndex) { List<Integer> preRow = new ArrayList();
preRow.add(1); //第一行
List<Integer> curRow = new ArrayList();
if (rowIndex == 0) {
return preRow;
} //如果不是第一行
curRow.add(1);
for (int i = 1; i <= rowIndex; ++i) {
//从第二行开始
curRow = new ArrayList<>();
for(int j = 0; j < i + 1; ++j) {
if(j == 0 || j == i) {
curRow.add(1);
} else {
curRow.add(preRow.get(j - 1) + preRow.get(j));
}
}
preRow = curRow;
} return curRow;
} public static void main(String args[]) {
GetRow getRow = new GetRow();
System.out.println(getRow.solution(0));
}
}

【LEETCODE】34、119题,Pascal's Triangle II的更多相关文章

  1. LeetCode(119) Pascal's Triangle II

    题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...

  2. LeetCode算法题-Pascal's Triangle II(Java实现)

    这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...

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

  4. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  5. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  6. LeetCode OJ 119. Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  7. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  8. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  9. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

随机推荐

  1. java调用jni oci接口宕机原因排查

    调用最简单的JNI没有出错,但是涉及到OCI时就会异常退出,分析后基本确定是OCI 11g中的signal所致,参考ora-24550 signo=6 signo=11解决. 但是这个相同的so库直接 ...

  2. openresty开发系列27--openresty中封装redis操作

    openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...

  3. 003-结构型-06-组合模式(Composite)

    一.概述 将对象组合成树形结构以表示“部分一整体”的层次结构 组合模式是为了表示那些层次结构,同时部分和整体也可能是一样的结构,常见的如文件夹或者树. 通过递归手段来构造树形的对象结构,并可以通过一个 ...

  4. (?:pattern) 与 (?=pattern)的区别

    共同点 (?:pattern) 与 (?=pattern)都匹配pattern,但不会把pattern结果放到Matches的集合中. 区别 (?:pattern) 匹配得到的结果包含pattern. ...

  5. sqoop import mysql to hive table:GC overhead limit exceeded

    1. Scenario description when I use sqoop to import mysql table into hive, I got the following error: ...

  6. [ kvm ] 嵌套虚拟化

    1. 前言 在学习 kvm 的过程中,需要在虚拟机中再次开启虚拟机,这里就需要使用到嵌套虚拟化,做个记录吧. 2. 配置嵌套虚拟化 2.1 查看物理机是否支持嵌套虚拟化 cat /sys/module ...

  7. 【Leetcode_easy】832. Flipping an Image

    problem 832. Flipping an Image solution1: class Solution { public: vector<vector<int>> f ...

  8. 使用mousedown、mousemove、mouseup实现拖拽效果

    如何实现一个元素的拖拽效果,使用原生的js实现,习惯了jquery的同学们,你们自己写了吗?N久使用mvvm框架,不写jquery的东西,感觉自己完全不会了. 话不多说,直接上code.本例子以简单的 ...

  9. Gitlab提交时间错误问题修复

    gitlab-ctl status gitlab提交时间显示错误,明明是近期修改提交的代码在页面显示的时间是19年前 查看配置文件 /etc/gitlab/gitlab.rb 时区设置正确,再说就算是 ...

  10. 03点睛Spring MVC 4.1-REST

    转发:https://www.iteye.com/blog/wiselyman-2214290 3.1 REST REST:Representational State Transfer; REST是 ...