
力扣500题刷题笔记8:前缀枚举与n个骰子点数概率计算
下载需积分: 0 | 1.63MB |
更新于2023-12-31
| 46 浏览量 | 举报
收藏
力扣500题刷题笔记8包含两个具体题目的解题思路和方法。第一个题目是要构造一个函数 f(prefix, n),来求出1~n中有多少个数的前缀是prefix。解题方法是最终答案的前缀开始枚举,不断减小k值,一位一位来看答。第二个题目是剑指 Offer 60中的问题,即求n个骰子扔在地上,所有骰子朝上一面的点数之和为s。解题思路是用动态规划来计算所有点数出现的概率。具体做法是先计算所有点数出现的总次数,然后使用状态表示f[i][j]来表示投掷i个骰子,点数之和为j出现的次数。计算时依据最后一次投掷的点数划分集合,即f[i][j] = f[i-1][j-k],k属于{1, 2, 3, 4, 5, 6}并且j>=k。最后是对数组进行初始化,投掷0个骰子,点数之和为0只有一种方案。最终输出的是包含所有点数的概率数组。
This summary covers two specific problems and solutions from the LeetCode 500 problem-solving notes. The first problem is to construct a function f(prefix, n) to find out how many numbers in the range of 1 to n have the prefix "prefix". The solution method involves enumerating the prefix of the final answer and continuously reducing the value of k to examine the answer digit by digit. The second problem is from the Sword Point Offer 60, which involves throwing n dice on the ground and finding the probability distribution of the sum of the points. The solution approach uses dynamic programming to calculate the probability of all point values. It begins by calculating the total number of occurrences of all point values and then uses the state representation f[i][j] to denote the number of occurrences when throwing i dice with a sum of j. The calculation revolves around dividing the set based on the last dice throw, namely f[i][j] = f[i-1][j-k], where k belongs to {1, 2, 3, 4, 5, 6} and j>=k. Initialization involves setting f[1][1,2,3,4,5,6] = 1 = f[0][0], as there is only one way of getting a sum of 0 when throwing 0 dice. The final output is an array containing the probability of all point values.
相关推荐










东郊椰林放猪散仙
- 粉丝: 26
最新资源
- U盘背景自定义工具:打造个性化桌面体验
- 初学者必看:仿QQ聊天界面设计与VC实现
- ASP.NET实现网银接口的示例与流程图
- 汇编语言课程习题解答指南
- 个人珍藏多年:超级经典网页特效大推荐
- 南京理工大学《矩阵分析》课件下载指南
- msp430 gcc编译器版本对比与下载指南
- 实用VFP通讯录管理与查询程序功能详解
- DonetUpgrade1.13:.NET程序增量部署与效率提升工具
- Linux常用命令源代码详解与集合分享
- Java网络协议技术内幕及源码解析
- 探究仿开心网PHP源码结构与功能解析
- CSS 2.0中文手册:便捷索引查询
- DOS环境下汇编编译器及基础小程序介绍
- uC/OS-II在ARM架构上的移植方法研究
- 获取JDOM资源包及官方源码指南
- delphi换肤控件BusinessSkinForm的137套精美皮肤集锦
- AjaxPro2实现页面函数与CS类的简易交互
- 初学者的电子设计全攻略:模拟、数字电路与单片机
- 经典cisco网络基础教程:设置详解
- ajaxPro 9.2.17.1解决脚本错误
- 基于Flash的网页录音功能与FMS配置指南
- jQuery中文用户手册与Starterkit入门指南
- 1Z0-051题库全面整合,PDF与Word版本完善