问题描述:

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

分析:编写程序,找到第n个ugly number。

//动态规划方法
//根据丑数的定义,丑数应该是另一个丑数乘以2、3或者5的结果(1除外)

/**
* 因此我们可以创建一个数组,里面的数字是排好序的丑数,里面的每一个丑数是前面的丑数乘以2、3或者5得到的。
* 关键就是保证数组里面的数字是排好序的。
* 假设arr[1..i]是已经排好序的数组,则arr[i]一定是这里面最大的数,那么我们只要去寻找新生成的数字中比arr[i]大的的最小的数。
* 新生成的数是由前面的数字*2或*3或*5得到的。我们定义index2为前面数字*2中的所有数字中满足大于arr[i]的最小的数的下标,index3,index5类似定义,
* 则应该放在arr[i+1]位置的数字便是min(arr[index2]*2,arr[index3]*3,arr[index5]*5)。
* index2,index3,index5是维持动态向前的,不会产生无效搜索,因为当前找的数字一定比原来找的要大,所以从上一次找到的下标开始进行搜索就可以了。

算法:

public static int findTheNthUglyNumber(int Mindex){
int index = 1;
int[] arr = new int[Mindex]; //存放排好序的ugly number
arr[0] = 1; //最小ugly number
int index2 = 0, index3 = 0, index5 = 0; //index2定义为前面数字*2的所有数字中满足大于arr[i]的最小数的下标,index3和index5的定义类似。
while(index < Mindex)
{
int min = Min(arr[index2] * 2,arr[index3] * 3,arr[index5] * 5); //这里要注意 乘以2,3,5
arr[index] = min;
//更新index2,index3,index5
while(arr[index2] * 2 <= arr[index]) index2++;
while(arr[index3] * 3 <= arr[index]) index3++;
while(arr[index5] * 5 <= arr[index]) index5++;
index++;
} // int ans = arr[Mindex - 1]; //获得第n个ugly number
return arr[index - 1 ];
} //寻找三者中的最小值
public static int Min(int a, int b , int c){
a = a < b ? a : b;
if(c < a) return c;
else return a;
}

Ugly Number II leetcode java的更多相关文章

  1. Ugly Number II -- LeetCode

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  2. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  3. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  4. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  5. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  6. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

  7. 【刷题-LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  8. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

  9. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

随机推荐

  1. POJ 1486 Sorting Slides(二分图完全匹配必须边)题解

    题意:给你n张照片的范围,n个点的坐标,问你能唯一确定那几个点属于那几张照片,例如样例中4唯一属于A,2唯一属于C,1唯一属于B,3唯一属于C 思路:进行二分图完全匹配,怎么判断唯一属于?匹配完之后删 ...

  2. (zhuan) Paper Collection of Multi-Agent Reinforcement Learning (MARL)

    this blog from: https://github.com/LantaoYu/MARL-Papers Paper Collection of Multi-Agent Reinforcemen ...

  3. (转)能根据文字生成图片的 GAN,深度学习领域的又一新星

    本文转自:https://mp.weixin.qq.com/s?__biz=MzIwMTgwNjgyOQ==&mid=2247484846&idx=1&sn=c2333a998 ...

  4. [CodeForces 892A] Greed (Java中sort实现从大到小排序)

    题目链接:http://codeforces.com/problemset/problem/892/A 具体的Java 中 sort实现降序排序:https://www.cnblogs.com/you ...

  5. C#Winform工具箱简介

    BindingSource:指定支持事务处理初始化Button:[按钮]用户单击它时引发事件 CheckBox:[复选框]允许用户选择或清除关联选项 CheckedListBox:[复选列表框]显示一 ...

  6. 17秋 SDN课程 第三次上机作业

    SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证

  7. BZOJ 1055: [HAOI2008]玩具取名(记忆化搜索)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1055 题意: 思路:记忆化搜索. #include<iostream> #include ...

  8. RN中关于IOS和Android的相关权限的问题

    在日常的开发中,时常需要去获取应用的一权限 比如查看通讯录/打开摄像机等 1:ios  iOS 的权限管理在info.plist里设置 info.plist主要是管理了app 的一些信息文件,比如版本 ...

  9. JS中什么是发布--订阅模式?

    转载文章部分内容: 发布订阅模式介绍 发布---订阅模式又叫观察者模式,它定义了对象间的一种一对多的关系,让多个观察者对象同时监听某一个主题对象,当一个对象发生改变时,所有依赖于它的对象都将得到通知. ...

  10. BZOJ 4591 【SHOI2015】 超能粒子炮·改

    题目链接:超能粒子炮·改 这道题的大体思路就是用\(lucas\)定理,然后合并同类项,就可以得到一个可以递归算的式子了. 我们用\(S(n,k)\)表示答案,\(p\)表示模数(\(2333\)是一 ...