zoukankan      html  css  js  c++  java
  • 0119. Pascal's Triangle II (E)

    Pascal's Triangle II (E)

    题目

    Given a non-negative index k where k ≤ 33, return the (k^{th}) index row of the Pascal's triangle.

    Note that the row index starts from 0.

    img
    In Pascal's triangle, each number is the sum of the two numbers directly above it.

    Example:

    Input: 3
    Output: [1,3,3,1]
    

    Follow up:

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


    题意

    求出帕斯卡(杨辉)三角形的指定行的元素。

    思路

    可以直接建二维数组进行模拟;也可以压缩至一维数组进行处理;最省空间的是直接根据杨辉三角形的组合数性质直接计算出指定行的所有元素,即 (triangle[i][j]=C^j_i)


    代码实现

    Java

    二维数组

    class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> ans = new ArrayList<>();
            int[][] triangle = new int[rowIndex + 1][rowIndex + 1];
            triangle[0][0] = 1;
    
            for (int i = 1; i <= rowIndex; i++) {
                for (int j = 0; j <= i; j++) {
                    triangle[i][j] = j == 0 || j == i ? 1 : triangle[i - 1][j - 1] + triangle[i - 1][j];
                }
            }
    
            for (int i = 0; i <= rowIndex; i++) {
                ans.add(triangle[rowIndex][i]);
            }
    
            return ans;
        }
    }
    

    一维数组

    class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> ans = new ArrayList<>();
            int[] row = new int[rowIndex + 1];
            row[0] = 1;
    
            for (int i = 1; i <= rowIndex; i++) {
                for (int j = i; j >= 1; j--) {
                    row[j] = row[j] + row[j - 1];
                }
            }
    
            for (int i = 0; i <= rowIndex; i++) {
                ans.add(row[i]);
            }
    
            return ans;
        }
    }
    

    一维数组(直接List处理)

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

    组合数

    class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> ans = new ArrayList<>();
    
            for (int i = 0; i <= rowIndex; i++) {
                ans.add(combination(rowIndex, i));
            }
    
            return ans;
        }
    
        private int combination(int i, int j) {
            if (j > i / 2) {
                return combination(i, i - j);
            }
    
            double ans = 1.0;
            while (j >= 1) {
                ans *= 1.0 * i-- / j--;
            }
    
            return (int) Math.round(ans);
        }
    }
    

    JavaScript

    /**
     * @param {number} rowIndex
     * @return {number[]}
     */
    var getRow = function (rowIndex) {
      let k = 0
      let tri = [1]
      while (k != rowIndex) {
        let pre = 1
        for (let i = 1; i <= k; i++) {
          let cur = tri[i]
          tri[i] = cur + pre
          pre = cur
        }
        tri[++k] = 1
      }
      return tri
    }
    
  • 相关阅读:
    lambda表达式查询经验:IN 和groupby的使用
    Sql server 查询指定时间区间工作日数、休息日数等日期操作
    ASP.NET MVC用存储过程批量添加修改数据
    .NET十五周年生日快乐 (3月7日发布Visual Studio 2017正式版?)
    浅谈 MVC中的ViewData、ViewBag和TempData
    ASP.NET给前端动态添加修改 CSS样式JS 标题 关键字
    元宵节大家来猜灯谜,祝元宵节快乐!
    Visual Studio 2017 RC 初探安装
    MySql存储过程的使用
    准备熟悉Kaggle -菜鸟进阶
  • 原文地址:https://www.cnblogs.com/mapoos/p/13494742.html
Copyright © 2011-2022 走看看