剑指offer 34_丑数
丑数:只有2 3 5 这三个因子的数,求前(第)1500个。习惯上我们把1当作第一个丑数
例如 6, 8是丑数。14不是。
#include <stdio.h>
int Min(int x,int y, int z){
int min = (x <= y) ? x : y;
return min = (z <= min) ? z : min;
}
int GetUglyNumber(int index){
if(index <= 0){
return 0;
}
int* uglyNumbers= new int[index];
int uglyCount = 1;
uglyNumbers[0] = 1;
int* ugly2 = uglyNumbers;
int* ugly3 = uglyNumbers;
int* ugly5 = uglyNumbers;
for(; uglyCount< index; ++uglyCount){
int min = Min((*ugly2)*2,(*ugly3)*3,(*ugly5)*5);
uglyNumbers[uglyCount] = min;
while((*ugly2)*2 <= min){
++ugly2;
}
while((*ugly3)*3 <= min){
++ugly3;
}
while((*ugly5)*5 <= min){
++ugly5;
}
}
for(int i=0; i<index; i++){
printf("%d\n",uglyNumbers[i]);
}
delete[] uglyNumbers;
}
int main(){
int index = 1500;
GetUglyNumber(index);
return 0;
}
gcc uglyNumber.cpp
1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
25
27
30
32
36
.
.
.
786432000
787320000
791015625
796262400
797161500
800000000
805306368
806215680
810000000
816293376
819200000
820125000
829440000
838860800
839808000
843750000
849346560
850305600
854296875
859963392
第1500个为 859963392
剑指offer 34_丑数的更多相关文章
- 剑指 Offer 49. 丑数 + 小根堆 + 动态规划
剑指 Offer 49. 丑数 Offer_49 题目详情 解法一:小根堆+哈希表/HashSet 根据丑数的定义,如果a是丑数,那么a2, a3以及a*5都是丑数 可以使用小根堆存储按照从小到大排序 ...
- 【剑指Offer】丑数 解题报告
[剑指Offer]丑数 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目描述: ...
- 力扣 - 剑指 Offer 49. 丑数
题目 剑指 Offer 49. 丑数 思路1 丑数是只包含 2.3.5 这三个质因子的数字,同时 1 也是丑数.要计算出 n 之前全部的丑数,就必须将 n 之前的每个丑数都乘以 2.3.5,选取出最小 ...
- 【剑指offer】丑数
把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. leetcode上也 ...
- 【Java】 剑指offer(49) 丑数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 我们把只包含因子2.3和5的数称作丑数(Ugly Number). ...
- Go语言实现:【剑指offer】丑数
该题目来源于牛客网<剑指offer>专题. 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7.习惯上我们把1当做是第一个丑 ...
- 《剑指offer》丑数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- 每日一题 - 剑指 Offer 49. 丑数
题目信息 时间: 2019-07-03 题目链接:Leetcode tag:动态规划 小根堆 难易程度:中等 题目描述: 我们把只包含质因子 2.3 和 5 的数称作丑数(Ugly Number).求 ...
- 剑指OFFER之丑数(九度OJ1214)
题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 输入: 输 ...
随机推荐
- android里getView,inflate,listview问题
今天在写一个listview的时候,遇到一个问题,如下 package com.brookji.funlearn; import java.util.ArrayList; import android ...
- 按键(ESC ,F1,F2等)——wpf的命令处理方法
WPF窗体的命令绑定 方法一:使用代码 <WpfUI:View.CommandBindings> <CommandBinding Command="Help" ...
- 解决Navicat连接mysql报错:1862 - Your password has expired. To log in you must change it using a client that supports expired passwords.
今天尝试用Navicat连接mysql时,发现一个1862的报错问题: 后来参照这篇文章https://blog.csdn.net/u010513756/article/details/5073501 ...
- RK30SDK系统重启源码分析
Linux系统重启的最底层函数是arch_reset,这是一个全局的函数指针变量,定义在 arch/arm/mach-rk30/include/mach/system.h中: extern void ...
- C#中的线程(三)多线程
C#中的线程(三)多线程 Keywords:C# 线程Source:http://www.albahari.com/threading/Author: Joe AlbahariTranslator ...
- LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 单链表输出倒数第k个结点值(栈实现)
思路1:定义两个指针变量p和q,初始时均指向头节点的下一个节点(链表的第一个节点),p指针沿链表移动: 当p指针移动到第k个节点时,q指针开始与p指针同步移动, 当p指针移动到最后一个节点时,q指针所 ...
- PostgreSQL学习手册 性能提升技巧
http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 一.使用EXPLAIN: PostgreSQL为每个查询都生成一个查询 ...
- LeetCode Maximum Average Subarray I
原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...
- How To Uninstall Software Using The Ubuntu Command Line
How To Uninstall Software Using The Ubuntu Command Line Uninstall Ubuntu Software Using The Terminal ...