【LeetCode】Pascal's Triangle II

Pascal's Triangle II 
Accepted: 11888 Total Submissions: 39509 My Submissions
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?
【解题思路】
1、参照Pascal's Triangle,算出所有的list,直接get(rowIndex)
2、如果是空间限制,只能声明k长度的list,依次计算。具体实现是这样的:
1)、初始化一个list
2)、每个列表的第一个值是1
3)、针对每个列表,长度应该是当前rowIndex+1
4)、那么列表元素值为当前值+前一个值,其实就是递推。
5)、循环刚开始,前一个值为1,以后前一个值需要记录。
6)、最后别忘了最后一个值是1
事实证明,限制空间,时间就会变长。

1、Java AC 360ms

public class Solution {
    public ArrayList<Integer> getRow(int rowIndex) {
        if(rowIndex < 0){
            return new ArrayList<Integer>();
        }
        return generate(rowIndex+1).get(rowIndex);
    }
    public ArrayList<ArrayList<Integer>> generate(int numRows) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        if(numRows <= 0){
            return list;
        }
        ArrayList<Integer> firstList = new ArrayList<Integer>();
        firstList.add(1);
        list.add(firstList);
        for(int i = 1; i < numRows; i++){
            ArrayList<Integer> secList = new ArrayList<Integer>();
            ArrayList<Integer> tempList = list.get(i-1);
            int size = tempList.size();
            secList.add(1);
            for(int j = 1; j < size; j++){
                secList.add(tempList.get(j-1) + tempList.get(j));
            }
            secList.add(1);
            list.add(secList);
        }
        return list;
    }
}
1、Python AC 136ms

class Solution:
    # @return a list of integers
    def getRow(self, rowIndex):
        return self.generate(rowIndex+1)[rowIndex]

    def generate(self, numRows):
        list = []
        if numRows <= 0:
            return []
        list.append([1])
        for i in range(1, numRows):
            tempList = list[i-1]
            rowList = []
            rowList.append(1)
            for j in range(1,len(tempList)):
                rowList.append(tempList[j-1] + tempList[j])
            rowList.append(1)
            list.append(rowList)
        return list
		
		
2、Java AC 392MS

public class Solution {
    public List<Integer> getRow(int rowIndex) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		for (int i = 1; i < rowIndex + 1; i++) {
			int curNum = 1;
			for (int j = 1; j < i; j++) {
				int temp = curNum;
				curNum = list.get(j);
				list.set(j, curNum + temp);
			}
			list.add(1);
		}
		return list;
	}

}
2、Python AC 152MS

class Solution:
    # @return a list of integers
    def getRow(self, rowIndex):
        list = [1]
        for i in range(1,rowIndex+1):
            curNum = 1
            for j in range(1, i):
                temp = curNum
                curNum = list[j]
                list[j] = temp + list[j]
            list.append(1)
        return list
		
		


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值