zoukankan      html  css  js  c++  java
  • 118. Pascal's Triangle

    package LeetCode_118
    
    import java.util.*
    
    /**
     * 118. Pascal's Triangle
     * https://leetcode.com/problems/pascals-triangle/description/
     * Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
     *
     * Example:
    Input: 5
    Output:
    [
        [1],
       [1,1],
      [1,2,1],
     [1,3,3,1],
    [1,4,6,4,1]
    ]
     * */
    class Solution {
        /*
        * Time complexity:O(n^2), Space complexity:O(n)
        * */
        fun generate(numRows: Int): List<List<Int>> {
            val result = ArrayList<ArrayList<Int>>()
            for (i in 0 until numRows){
                val row = ArrayList<Int>()
                for(j in 0 until i+1){
                    if (j==0 || j==i){
                        //insert 1 into head and tail
                        row.add(1)
                    } else {
                        //the value of current position is from it's top left + top right
                        row.add(result.get(i - 1).get(j) + result.get(i - 1).get(j - 1))
                    }
                }
                result.add(row)
            }
            return result
        }
    }
  • 相关阅读:
    总结:关于作用域的经典面试题
    解决JS拖拽出现的问题
    JS正则(3)总结
    JS正则(2)对正则的理解
    JS 闭包 正则(1)
    JS Date对象
    笔记
    9.13笔记
    9.12学习笔记
    9.11学习笔记
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13620340.html
Copyright © 2011-2022 走看看