题目描述:
Given an index k, return the kth row of the Pascal‘s triangle.
For example, given k = 3,
Return [1,3,3,1]
.
解题思路:
每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数。
代码描述:
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> result = new ArrayList<Integer>(); for(int i = 0; i <= rowIndex; i++){ result.add(0,1); for(int j = 1; j < i; j++){ result.set(j, result.get(j) + result.get(j + 1)); } } return result; } }
Java [Leetcode 119]Pascal's Triangle II
时间: 2024-12-26 17:51:46