问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

输入: 3

输出: [1,3,3,1]

你可以优化你的算法到 O(k) 空间复杂度吗?


Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.


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

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

Input: 3

Output: [1,3,3,1]


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

public class Program {public static void Main(string[] args) {var res = GetRow(4);var res2 = GetRow2(5);ShowArray(res);ShowArray(res2);Console.ReadKey();}private static void ShowArray(IList<int> array) {foreach(var num in array) {Console.Write($"{num} ");}Console.WriteLine();}private static IList<int> GetRow(int rowIndex) {int[][] res = new int[rowIndex + 1][];for(int i = 0; i < res.Length; i++) {res[i] = new int[rowIndex + 1];}res[0][0] = 1;for(int i = 1; i < rowIndex + 1; i++) {res[i][0] = 1;for(int j = 1; j < i + 1; j++) {res[i][j] = res[i - 1][j - 1] + res[i - 1][j];}}return res[rowIndex];}private static IList<int> GetRow2(int rowIndex) {int[] res = new int[rowIndex + 1];res[0] = 1;for(int i = 1; i < rowIndex + 1; i++) {for(int j = rowIndex; j >= 1; j--) {res[j] = res[j - 1] + res[j];}}return res;}}

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3690 访问。

1 4 6 4 1
1 5 10 10 5 1

分析:

显而易见,GetRow在最坏的情况下的时间复杂度为:  ,空间复杂度也为: ;

GetRow2在最坏的情况下的时间复杂度为:  ,由于使用一维数组空间复杂度为:  。

GetRow2方法可以根据杨辉三角的对称性优化,只需计算一半即可,其实现方法留给各位看官。

C#LeetCode刷题之#119-杨辉三角 II(Pascal‘s Triangle II)相关推荐

  1. 杨辉三角(Pascal‘s Triangle)

    6.杨辉三角(Pascal's Triangle) ​ 1.每个数等于它上方两数之和[直角模式等于上方和左上方元素之和]. ​ 2.每行数字左右对称,由1开始逐渐变大. ​ 3.第一行有1个元素,第n ...

  2. Leetcode题库 119.杨辉三角(单数组迭代 C实现)

    文章目录 解析 思路 效果 代码 解析 ret:存储迭代数组 思路 ret数组: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 -- 迭代公式:ret[i]= ...

  3. python杨辉三角_yiduobo的每日leetcode 118.杨辉三角 amp;amp; 119.杨辉三角II

    祖传的手艺不想丢了,所以按顺序写一个leetcode的题解.计划每日两题,争取不卡题吧. 118.杨辉三角https://leetcode-cn.com/problems/pascals-triang ...

  4. 杨辉三角 118.杨辉三角 119.杨辉三角Ⅱ(数学解法)

    118.杨辉三角 public List<List<Integer>> generate(int numRows) {if (numRows == 0) return new ...

  5. leetcode 刷题 119. 杨辉三角II

    给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 解答: class Soluti ...

  6. leetcode 119. 杨辉三角 II

    题目 思路 根据上一题题解:leetcode 118. 杨辉三角 本题直接取第 i 行返回就可以了 题解 1.续上一题(118题)的解法,上一题的代码直接拿来用了 import java.util.A ...

  7. 【Leetcode每日一题】118. 杨辉三角(水题)

    Leetcode每日一题 题目链接: 118. 杨辉三角 难度: 简单 解题思路: 无.见代码. 题解: class Solution:def generate(self, numRows: int) ...

  8. C#LeetCode刷题之#118-杨辉三角(Pascal‘s Triangle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3688 访问. 给定一个非负整数 numRows,生成杨辉三角的前 ...

  9. 杨辉三角java代码_【LeetCode】118. 杨辉三角(Pascal#x27;s Triangle)解题思路

    题目如下(题目链接戳我): 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 备注:在杨辉三角中,每个数是它左上方和右上方的数的和.示例: 输入: 5 输出: [[1],[1, ...

  10. 2017百度之星初赛B场第一题Chess--简单杨辉三角问题

    Chess  Accepts: 1799  Submissions: 5738  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 32768 ...

最新文章

  1. linux 误删除mysql表能恢复吗_Linux下Oracle误删除数据文件恢复操作
  2. 磁盘格式化、磁盘挂载、手动增加swap空间
  3. 为什么要使用String.Equals over ==? [重复]
  4. python3 元组 tuple 操作
  5. 3.20周记:栈和队列
  6. 1.2 Java类的定义
  7. 类DefaultWsdl 11定义中英文对比API文档
  8. 锐捷EG易网关远程命令执行漏洞-1
  9. cpu影响matlab仿真速度吗,Proteus仿真速度很慢的分析
  10. 云栖专辑| 阿里毕玄:程序员的成长路线
  11. 使用SpringMVC模拟文件上传与下载案例
  12. vmware view由哪些组件组成?
  13. X-UA-Compatible 解决IE浏览器样式不兼容问题
  14. leetcode 870.优势洗牌
  15. cip协议服务器,控制及信息协议(CIP)
  16. Golang channel 快速入门
  17. Ubuntu16.04装机2:安装CUDA10.2+cuDNN7.6.5
  18. 在线计算CAN波特率参数
  19. C程序设计语言——基础概念
  20. 那些年啊,那些事——一个程序员的奋斗史 ——16

热门文章

  1. Linux已经霸占了服务器领域
  2. 一篇弄懂 HTTP和HTTPS基本关系
  3. 【STL学习】堆相关算法详解与C++编程实现(Heap)
  4. 异常捕获try...catch... c#
  5. 环境变量的配置windows10系统
  6. dj电商-需求分析开始-静态资源-用户模块
  7. django-行对向的反向查找
  8. django-基本使用
  9. jquery-手风琴效果
  10. python-运算符-比较运算符