publicstatic List<Integer>method1(int numsRows){
ArrayList<List<Integer>> resList =newArrayList<>();for(int i =0; i <= numsRows; i++){// 这里必须加等号,否则少遍历循环一轮
ArrayList<Integer> tempList =newArrayList<>();for(int j =0; j <= i ; j++){if(j == i||j==0){
tempList.add(1);}else{
tempList.add(resList.get(i-1).get(j-1)+resList.get(i-1).get(j));}}
resList.add(tempList);}return resList.get(numsRows);// 从0开始的行数}
买卖股票的最佳时机2
publicstaticintmaxProfit2(int[] prices){if(prices.length ==1|| prices == null){// 如果只有一个数不存在昨日持股额return0;}int res =0;for(int i =1;i<prices.length;i++){if(prices[i]>prices[i-1]){
res +=(prices[i]-prices[i-1]);// 如果当前持股比昨日持股多,立即将差值累加。}}return res;}