博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指Offer——丑数
阅读量:4227 次
发布时间:2019-05-26

本文共 1023 字,大约阅读时间需要 3 分钟。

题目描述:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

import java.lang.Math.*;public class Solution {    /*    思路1:对于从1开始的每个数,都循环的对2,取余数,如果最后的余数不为0 ,则对3循环的取余数         如果左后的余数不为0 ,则对5取余数,如果最后的余数不为0 ,则该数不是丑数,否则对丑数         计数的变量count加1。优点是直观且简单,缺点是实现效率低,因为一个数不管是不是丑数都要计算    思路2:因为丑数只能有2、3、5这三种因子,因此丑数肯定可以分解为两个丑数的乘积,而不能分解为非丑数的         乘积,因此可以把之前已经找到的丑数保存下来,然后从头开始对每个丑数乘以2、3、5直到找到一个         大于当前最大丑数的最小值,就是下一个丑数         但是这样乘下来,当N很大的时候,前面部分的丑数乘以2、3、5都是小于当前丑数的,那么我们可以找到         一个丑数,在他前面的丑数乘以2都是小于当前丑数的,在他之后的丑数乘以2都是大于当前丑数的,把这         个数称为T2,同理找到T3、T5,然后这三个数里面最小的就是下一个丑数         我们用一个数组来存储之前已经找到的丑数,那么T2就是:大于(当前丑数/2)的第一个丑数乘以2,T3,T5同理    */    public int GetUglyNumber_Solution(int index) {        if(index<=0){            return 0;        }        //定义一个整数来对丑数的个数进行计数        int count = 0;        //1是第一个丑数        int uglyNum = 1;        int t2=0;        int t3=0;        int t5=0;        //用一个数组来保存已经找到的丑数        int[] uglyNums = new int[index];        uglyNums[0] = 1;        while(count

转载地址:http://gsnqi.baihongyu.com/

你可能感兴趣的文章
Accelerated VB 2005
查看>>
Pro Access 2007
查看>>
Excel 2007: Beyond the Manual
查看>>
Windows Vista: Beyond the Manual
查看>>
DotNetNuke For Dummies
查看>>
PCI Compliance: Understand and Implement Effective PCI Data Security Standard Compliance
查看>>
Flash CS3 For Dummies
查看>>
Professional ASP.NET 2.0 AJAX
查看>>
Security+ Study Guide
查看>>
Programming Interviews Exposed: Secrets to Landing Your Next Job
查看>>
Linksys WRT54G Ultimate Hacking
查看>>
Professional Rootkits
查看>>
Financial Applications using Excel Add-in Development in C/C++
查看>>
Learning Joomla! Extension Development: Creating Modules, Components, and Plugins with PHP
查看>>
How to Cheat at IIS 7 Server Administration
查看>>
Simply JavaScript
查看>>
Expert SQL Server 2005 Integration Services
查看>>
Beginning SharePoint 2007: Building Team Solutions with MOSS 2007
查看>>
QoS Over Heterogeneous Networks
查看>>
Workflow in the 2007 Microsoft Office System
查看>>