Pascal's Triangle II

mac2022-06-30  77

题目:

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,Return [1,3,3,1].

Note:Could you optimize your algorithm to use only O(k) extra space?

 

Code:

public class Solution { public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> result = new ArrayList<Integer>(); if(rowIndex<0)return result; result.add(1); for(int i=1;i<=rowIndex;i++) { for (int j=result.size()-2;j>=0;j--) { result.set(j+1,result.get(j)+result.get(j+1)); //****Good Idea!!! } result.add(1); } return result; } }

 

转载于:https://www.cnblogs.com/hygeia/p/4618540.html

最新回复(0)