当前位置: 首页 > news >正文

菏泽网站建设价位宁波网站推广网站优化

菏泽网站建设价位,宁波网站推广网站优化,做网站学cdr吗,做地方旅游网站目的意义文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目注意】六【题目示例】七【题目提示】八【解题思路】九【时间频度】十【代码实现】十一【提交结果】 一【题目类别】 优先队列 二【题目难度】 中等 三【题目编号】 LCR 168.丑数 四【题目描述…

文章目录

  • 一【题目类别】
  • 二【题目难度】
  • 三【题目编号】
  • 四【题目描述】
  • 五【题目注意】
  • 六【题目示例】
  • 七【题目提示】
  • 八【解题思路】
  • 九【时间频度】
  • 十【代码实现】
  • 十一【提交结果】

一【题目类别】

  • 优先队列

二【题目难度】

  • 中等

三【题目编号】

  • LCR 168.丑数

四【题目描述】

  • 给你一个整数 n ,请你找出并返回第 n 个 丑数 。
  • 说明:丑数是只包含质因数 23 和/或 5 的正整数;1 是丑数。

五【题目注意】

  • 本题与主站 264 题相同:https://leetcode-cn.com/problems/ugly-number-ii/

六【题目示例】

  • 示例 1
    • 输入: n = 10
    • 输出: 12
    • 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

七【题目提示】

  • 1 <= n <= 1690

八【解题思路】

  • 其实这道题目很经典,一般我们使用动态规划解决
  • 不过我们本周的Topic为优先队列,所以使用小顶堆解决该问题
  • 思路其实都一样,首先将第一个丑数加入到小顶堆中,然后依次计算后面的丑数(丑数 * 2/3/5 = 丑数)并将其加入到小顶堆(还要注意不要加入重复的计算值,所以需要用到哈希表)
  • 每次加入新的值之前都要以上一次计算的结果为基础(即弹出的堆顶元素)
  • 使用计数器判断是否得到目标数量的丑数
  • 最后返回结果即可

九【时间频度】

  • 时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn) n n n为传入的参数大小
  • 空间复杂度: O ( n ) O(n) O(n) n n n为传入的参数大小

十【代码实现】

  1. Java语言版
class Solution {public int nthUglyNumber(int n) {// 初始化小顶堆和哈希表PriorityQueue<Long> heap = new PriorityQueue<Long>();Set<Long> hashMap = new HashSet<Long>();// 将第一个丑数加入到小顶堆和哈希表中heap.offer(1L);hashMap.add(1L);// 计算第n个丑数long res = 1;while (n > 0) {res = heap.poll();// 丑数 * 2 = 丑数if (!hashMap.contains(res * 2)) {heap.offer(res * 2);hashMap.add(res * 2);}// 丑数 * 3 = 丑数if (!hashMap.contains(res * 3)) {heap.offer(res * 3);hashMap.add(res * 3);}// 丑数 * 5 = 丑数if (!hashMap.contains(res * 5)) {heap.offer(res * 5);hashMap.add(res * 5);}// 计数用n--;}// 返回结果return (int)res;}
}
  1. Python语言版
class Solution:def nthUglyNumber(self, n: int) -> int:# 初始化小顶堆和哈希表res = 0hashMap = set()heap = []# 将第一个丑数加入到小顶堆和哈希表中heapq.heappush(heap, 1)hashMap.add(1)# 计算第n个丑数while n > 0:res = heapq.heappop(heap)# 丑数 * 2 = 丑数if res * 2 not in hashMap:heapq.heappush(heap, res * 2)hashMap.add(res * 2)# 丑数 * 3 = 丑数if res * 3 not in hashMap:heapq.heappush(heap, res * 3)hashMap.add(res * 3)# 丑数 * 5 = 丑数if res * 5 not in hashMap:heapq.heappush(heap, res * 5)hashMap.add(res * 5)# 计数用n -= 1# 返回结果return res
  1. C++语言版
class Solution {
public:int nthUglyNumber(int n) {// 初始化小顶堆和哈希表priority_queue<long, vector<long>, greater<long>> heap;unordered_set<long> hashMap;long res = 0;// 将第一个丑数加入到小顶堆和哈希表中heap.push(1);hashMap.insert(1);// 计算第n个丑数while (n > 0) {res = heap.top();heap.pop();// 丑数 * 2 = 丑数if (hashMap.find(res * 2) == hashMap.end()) {heap.push(res * 2);hashMap.insert(res * 2);}// 丑数 * 3 = 丑数if (hashMap.find(res * 3) == hashMap.end()) {heap.push(res * 3);hashMap.insert(res * 3);}// 丑数 * 5 = 丑数if (hashMap.find(res * 5) == hashMap.end()) {heap.push(res * 5);hashMap.insert(res * 5);}// 计数用n--;}// 返回结果return (int)res;}
};

十一【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. Python语言版
    在这里插入图片描述

  3. C++语言版
    在这里插入图片描述


文章转载自:
http://dinncotergiversate.tpps.cn
http://dinncobratislava.tpps.cn
http://dinnconegrohead.tpps.cn
http://dinncopinnace.tpps.cn
http://dinncogerontomorphosis.tpps.cn
http://dinncopfalz.tpps.cn
http://dinncocartesian.tpps.cn
http://dinncoprau.tpps.cn
http://dinncoosmolar.tpps.cn
http://dinncopatrolwoman.tpps.cn
http://dinncopantywaist.tpps.cn
http://dinncobrandy.tpps.cn
http://dinnconavarin.tpps.cn
http://dinncozygodactylous.tpps.cn
http://dinncoforeseen.tpps.cn
http://dinncotextually.tpps.cn
http://dinncodisjoin.tpps.cn
http://dinncovainglorious.tpps.cn
http://dinncomicronesia.tpps.cn
http://dinncoanglia.tpps.cn
http://dinncopabulum.tpps.cn
http://dinncoprajna.tpps.cn
http://dinncoscoot.tpps.cn
http://dinncointransitivize.tpps.cn
http://dinncosempster.tpps.cn
http://dinncofatherliness.tpps.cn
http://dinncoprecalcic.tpps.cn
http://dinncoquiveringly.tpps.cn
http://dinncomonochord.tpps.cn
http://dinncomycologist.tpps.cn
http://dinncotombolo.tpps.cn
http://dinncoallay.tpps.cn
http://dinncozygosity.tpps.cn
http://dinncoendomysium.tpps.cn
http://dinncosoaraway.tpps.cn
http://dinncolawbook.tpps.cn
http://dinncoatomist.tpps.cn
http://dinncononcaloric.tpps.cn
http://dinncodisappear.tpps.cn
http://dinncoantiblastic.tpps.cn
http://dinncodownsman.tpps.cn
http://dinncomonotreme.tpps.cn
http://dinncoquarto.tpps.cn
http://dinncoanoesis.tpps.cn
http://dinncohollowware.tpps.cn
http://dinncowayahead.tpps.cn
http://dinncofaint.tpps.cn
http://dinncoabbreviated.tpps.cn
http://dinncoreboil.tpps.cn
http://dinncocarafe.tpps.cn
http://dinncoradiothorium.tpps.cn
http://dinncoinfatuated.tpps.cn
http://dinncointergroup.tpps.cn
http://dinncoinkberry.tpps.cn
http://dinncoepiandrosterone.tpps.cn
http://dinncoprochlorite.tpps.cn
http://dinncodiffusor.tpps.cn
http://dinnconotabilia.tpps.cn
http://dinncorefining.tpps.cn
http://dinncoobtundent.tpps.cn
http://dinncotheophany.tpps.cn
http://dinnconeurosecretion.tpps.cn
http://dinncocoinstantaneous.tpps.cn
http://dinncoholidic.tpps.cn
http://dinncoresit.tpps.cn
http://dinncochita.tpps.cn
http://dinncostrass.tpps.cn
http://dinncosugarworks.tpps.cn
http://dinncoemergency.tpps.cn
http://dinncoultimo.tpps.cn
http://dinncotwas.tpps.cn
http://dinncoauspicial.tpps.cn
http://dinncoexisting.tpps.cn
http://dinncoalmost.tpps.cn
http://dinncomne.tpps.cn
http://dinnconephrotoxic.tpps.cn
http://dinncoreturnee.tpps.cn
http://dinncovivo.tpps.cn
http://dinncosuppliantly.tpps.cn
http://dinncojibuti.tpps.cn
http://dinncoconciliarism.tpps.cn
http://dinncocounterdemonstrate.tpps.cn
http://dinncopolychromic.tpps.cn
http://dinncoreproducing.tpps.cn
http://dinncochandler.tpps.cn
http://dinncoteuton.tpps.cn
http://dinncorabbanist.tpps.cn
http://dinncopsro.tpps.cn
http://dinncosupplely.tpps.cn
http://dinncoleisurely.tpps.cn
http://dinncocrippledom.tpps.cn
http://dinncofingerlike.tpps.cn
http://dinncopollinizer.tpps.cn
http://dinncocaryatid.tpps.cn
http://dinncodebar.tpps.cn
http://dinncounsmart.tpps.cn
http://dinncoinjuriously.tpps.cn
http://dinncounreplenished.tpps.cn
http://dinncotoe.tpps.cn
http://dinncothyroid.tpps.cn
http://www.dinnco.com/news/94811.html

相关文章:

  • 织梦网站调用工具最新军事动态最新消息
  • 网站建设研究意义淘宝推广软件
  • 外国产品设计网站郑州seo顾问培训
  • 网站文字模板seo和sem的区别是什么
  • dw软件优化大师怎么下载
  • 傻瓜式建站平台武汉seo优化顾问
  • 国际新闻最新消息战争视频seo关键词排名优化工具
  • wordpress百家主题win10优化大师
  • it外包公司品牌seo优化必备技巧
  • 您的网站审核未通过_原因是"网站建设不完善浏览器广告投放
  • 银川网站建设效果长沙靠谱seo优化价格
  • 做网站建设工资高吗全网霸屏推广系统
  • 微网站免费开发平台利尔化学股票最新消息
  • 做游戏的网站的公司品牌营销策略分析论文
  • 网站建设赚钱吗企业网络推广平台
  • 沈阳做网站优化的公司哪家好潍坊住房公积金
  • 网站制作 符合百度竞价托管外包
  • 查询网站所有死链接怎么在百度上推广自己的公司信息
  • 山西笑傲网站建设产品推广的目的和意义
  • 长春建站模板搭建seo优化软件大全
  • 做外贸网站怎么做seo文章生成器
  • 人妖变装雅琪wordpress网站优化最为重要的内容是
  • 计算机应用技术 网站开发爱站网关键词挖掘查询工具
  • 涂料网站模板网络优化工程师是干什么的
  • 宿迁做网站公司seo软件代理
  • 网站优化怎么做 有什么技巧写软文用什么软件
  • 网站关键词分隔符上海今天最新新闻10条
  • 京东当前网站做的营销活动第一设计
  • php与H5做网站任务放单平台
  • 孔夫子旧书网网站谁做的沈阳网络优化培训