网站开发技术期末考试试题,最知名的网站推广公司,佛山cms建站系统,淄博找能做网站的公司118. 杨辉三角 
难度#xff1a;简单 
题目 
给定一个非负整数 *numRows#xff0c;*生成「杨辉三角」的前 numRows 行。 
在「杨辉三角」中#xff0c;每个数是它左上方和右上方的数的和。 示例 1: 
输入: numRows  5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]示例…118. 杨辉三角 
难度简单 
题目 
给定一个非负整数 *numRows*生成「杨辉三角」的前 numRows 行。 
在「杨辉三角」中每个数是它左上方和右上方的数的和。 示例 1: 
输入: numRows  5
输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]示例 2: 
输入: numRows  1
输出: [[1]]提示: 
1  numRows  30 
个人题解 
思路 
分析每层有i个元素一共有i行 
每层的第一个和最后一个元素必定为1其他元素都是上一层元素的当前索引位置和当前索引位置前一个 
遍历时考虑好上面两种情况即可 
class Solution {public ListListInteger generate(int numRows) {ListListInteger ans  new ArrayList();int times;for (int i  0; i  numRows; i) {ListInteger curLevelList  new ArrayList();times  i  1;for (int j  0; j  times; j) {if (j0 || j  times - 1) {curLevelList.add(1);} else {curLevelList.add(ans.get(i - 1).get(j-1)  ans.get(i - 1).get(j));}}ans.add(curLevelList);}return ans;}
}进阶 
上面层的概念是用list来表示的分析一下 如果用一个指针指向下一层的头节点即可得到每层遍历的起点再考虑用一个尾指针表示下一层的尾节点则遍历当前层时即可将下一层的节点接在尾节点上 经过上述分析遍历过程不再需要list容器只需要3个指针即可当前层遍历指针下一层头指针及下一层尾指针每次遍历完当前层将下一层头指针及尾指针重置 
class Solution {public Node connect(Node root) {Node curTail  root;Node nextHead  null;Node nextTail  null;while (curTail ! null) {// 看左子结点if (curTail.left ! null) {if (nextTail ! null) {nextTail.next  curTail.left;} else {nextHead  curTail.left;}nextTail  curTail.left;}// 看右子结点if (curTail.right ! null) {if (nextTail ! null) {nextTail.next  curTail.right;} else {nextHead  curTail.right;}nextTail  curTail.right;}if (curTail.next ! null) {// 继续当前层遍历curTail  curTail.next;} else {// 当前层遍历完毕开启下一层遍历将下一层指针重置curTail  nextHead;nextHead  null;nextTail  null;}}return root;}
}不讲武德题解 
class Solution {public ListListInteger generate(int numRows) {Integer[][] a {{1},{1, 1},{1, 2, 1},{1, 3, 3, 1},{1, 4, 6, 4, 1},{1, 5, 10, 10, 5, 1},{1, 6, 15, 20, 15, 6, 1},{1, 7, 21, 35, 35, 21, 7, 1},{1, 8, 28, 56, 70, 56, 28, 8, 1},{1, 9, 36, 84, 126, 126, 84, 36, 9, 1},{1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1},{1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1},{1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1},{1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1},{1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1},{1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1},{1, 16, 120, 560, 1820, 4368, 8008, 11440, 12870, 11440, 8008, 4368, 1820, 560, 120, 16, 1},{1, 17, 136, 680, 2380, 6188, 12376, 19448, 24310, 24310, 19448, 12376, 6188, 2380, 680, 136, 17, 1},{1, 18, 153, 816, 3060, 8568, 18564, 31824, 43758, 48620, 43758, 31824, 18564, 8568, 3060, 816, 153, 18, 1},{1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378, 92378, 75582, 50388, 27132, 11628, 3876, 969, 171, 19, 1},{1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756, 167960, 125970, 77520, 38760, 15504, 4845, 1140, 190, 20, 1},{1, 21, 210, 1330, 5985, 20349, 54264, 116280, 203490, 293930, 352716, 352716, 293930, 203490, 116280, 54264, 20349, 5985, 1330, 210, 21, 1},{1, 22, 231, 1540, 7315, 26334, 74613, 170544, 319770, 497420, 646646, 705432, 646646, 497420, 319770, 170544, 74613, 26334, 7315, 1540, 231, 22, 1},{1, 23, 253, 1771, 8855, 33649, 100947, 245157, 490314, 817190, 1144066, 1352078, 1352078, 1144066, 817190, 490314, 245157, 100947, 33649, 8855, 1771, 253, 23, 1},{1, 24, 276, 2024, 10626, 42504, 134596, 346104, 735471, 1307504, 1961256, 2496144, 2704156, 2496144, 1961256, 1307504, 735471, 346104, 134596, 42504, 10626, 2024, 276, 24, 1},{1, 25, 300, 2300, 12650, 53130, 177100, 480700, 1081575, 2042975, 3268760, 4457400, 5200300, 5200300, 4457400, 3268760, 2042975, 1081575, 480700, 177100, 53130, 12650, 2300, 300, 25, 1},{1, 26, 325, 2600, 14950, 65780, 230230, 657800, 1562275, 3124550, 5311735, 7726160, 9657700, 10400600, 9657700, 7726160, 5311735, 3124550, 1562275, 657800, 230230, 65780, 14950, 2600, 325, 26, 1},{1, 27, 351, 2925, 17550, 80730, 296010, 888030, 2220075, 4686825, 8436285, 13037895, 17383860, 20058300, 20058300, 17383860, 13037895, 8436285, 4686825, 2220075, 888030, 296010, 80730, 17550, 2925, 351, 27, 1},{1, 28, 378, 3276, 20475, 98280, 376740, 1184040, 3108105, 6906900, 13123110, 21474180, 30421755, 37442160, 40116600, 37442160, 30421755, 21474180, 13123110, 6906900, 3108105, 1184040, 376740, 98280, 20475, 3276, 378, 28, 1},{1, 29, 406, 3654, 23751, 118755, 475020, 1560780, 4292145, 10015005, 20030010, 34597290, 51895935, 67863915, 77558760, 77558760, 67863915, 51895935, 34597290, 20030010, 10015005, 4292145, 1560780, 475020, 118755, 23751, 3654, 406, 29, 1}};ListListInteger list  new ArrayList();for (int i  0; i  numRows; i) {list.add((ListInteger)Arrays.asList(a[i]));}return list;}
}
 文章转载自: http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.rmtxp.cn.gov.cn.rmtxp.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.rtlg.cn.gov.cn.rtlg.cn