LeetCode 数据结构—杨辉三角
2021-09-25 14:55
591 查看
我们可以知道前一排的数字可以直接影响到后一排的取值,且从第一排开始后,之后的首尾都是1.所以如果暴力的话,也可以直接得到正确结果。
public List<List<Integer>> generate(int numRows) { List<List<Integer>> res=new LinkedList<List<Integer>>(); List<Integer> index=new LinkedList<>(); index.add(1); res.add(index); if(numRows==1) return res; for(int i=1;i<numRows;i++){ List<Integer> LastRow=res.get(i-1); List<Integer> thisRow=new LinkedList<>(); thisRow.add(1); for(int j=0;j<LastRow.size()-1;j++) { thisRow.add(LastRow.get(j)+LastRow.get(j+1)); } thisRow.add(1); res.add(thisRow); } return res; }
相关文章推荐
- 【数据结构之List::iterator】leetcode LRUCache
- leetcode【数据结构简介】《二叉搜索树》卡片——二叉搜索树中的基本操作
- leetcode【数据结构简介】《链表》卡片——单链表
- leetcode【数据结构简介】《数组和字符串》卡片——小结
- Java数据结构与算法(15) - ch06递归(杨辉三角triangle)
- 数据结构与算法leetcode 刷题008 栈和队列的内容
- 数据结构与算法[LeetCode]—Combinations 求1至n中所有K位数的所有组合
- leetcode【数据结构简介】《二叉搜索树》卡片——二叉搜索树简介
- leetcode【数据结构简介】《数组和字符串》卡片——双指针技巧
- 数据结构与算法[LeetCode]4Sum
- leetcode【数据结构简介】《二叉树》卡片——总结
- leetcode【数据结构简介】《数组和字符串》卡片——字符串简介
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
- leetcode【数据结构简介】《二叉树》卡片——运用递归解决问题
- leetcode【数据结构简介】《数组和字符串》卡片——二维数组简介
- 数据结构与算法 leetcode 刷题010
- 数据结构与算法以及leetcode刷题 012 二分搜索树
- leetcode【数据结构简介】《二叉树》卡片——树的遍历
- leetcode【数据结构简介】《数组和字符串》卡片——数组简介
- 数据结构与算法[LeetCode]—Container With Most Water