当前位置:网站首页>2022/08/02------Ugly number
2022/08/02------Ugly number
2022-08-03 18:09:00 【city pig】
题目描述
解题思路
解题思路一:Make judgments while increasing numbers,Make the most of the ugly numbers you saved earlier.缺点:超出时间限制.
优化思路:It should not be judged numerically one by one,To take full advantage of the big ugly numbers are composed of the clown numbers,to get beforenThe specific value of the number.Avoid useless judgments,优化时间效率.
代码实现
思路一代码实现:
package cz;
import java.util.ArrayList;
import java.util.List;
public class NthUglyNumber_0802 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=11;
System.out.print(nthUglyNumber(n));
}
public static int nthUglyNumber(int n) {
List<Integer> mlist=new ArrayList<>();
int count=1;
int res=1;
mlist.add(res);
while(true) {
if(count==n)break;
else {
res+=1;
if(res%2==0&&mlist.contains(res/2)||res%3==0&&mlist.contains(res/3)||res%5==0&&mlist.contains(res/5)) {
count++;
mlist.add(res);
}
}
}
return res;
}
}
The optimization idea code is implemented as follows:
package cz;
import java.util.ArrayList;
import java.util.List;
public class NthUglyNumber_0802 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=11;
System.out.print(nthUglyNumber(n));
}
public static int nthUglyNumber(int n) {
int[] ugly=new int [n];
ugly[0]=1;
int i=0,j=0,k=0;
for(int index=1;index<n;index++)
{
int temp=Math.min(ugly[i]*2, Math.min(ugly[j]*3, ugly[k]*5));
//避免重复
if(temp==ugly[i]*2)i++;
if(temp==ugly[j]*3)j++;
if(temp==ugly[k]*5)k++;
ugly[index]=temp;
}
return ugly[n-1];
}
}
边栏推荐
- 快手通过国际权威信息安全和隐私保护认证,安全能力达到国际领先水平
- Share 14 JS functions you must know
- 【汇编语言03】第2章 寄存器——实验1:查看CPU和内存,用机器指令和汇编指令编程
- 三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
- 341. Flatten Nested List Iterator
- 【Django-Docker】Sqlite3.db读取权限不够-20220803
- Cool open technology x StarRocks: unified OLAP analysis engine, comprehensive building digital model of OTT
- 并查集模板及思想
- 图像质量指标:PSNR、SSIM、MSE
- cdc抽取mysql整个实例的binlog,有没有方案通过配置的方式将这些库表拆开分发到kafka
猜你喜欢
随机推荐
【牛客在线OJ】-字符逆序
rhel8.3 系统下修改有线网卡配置信息实现联网
LyScript 内存交换与差异对比
图像传感第一章学习心得
【技术白皮书】第二章:OCR智能文字识别回顾——自然语言文本发展历程
想要防止数据泄漏,如何选择国产浏览器?
Unable to start SinkRunner: { policy:org.apache.flume
mysql之的执行计划
PHP基础笔记-NO.1
openresty 高可用部署
2020icpc亚洲区域赛(济南)M题Cook Pancakes(小根堆的应用)
cell delay和net delay
Postgresql 备份大小情况!
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes), problem: (D) Magical Array
EasyNTS上云网关断电重启后设备离线是什么原因?
Interpretation of the paper (JKnet) "Representation Learning on Graphs with Jumping Knowledge Networks"
开篇-开启全新的.NET现代应用开发体验
你想知道的 Watch App 开发
DataWorks 标准版怎样实现SQL代码的复用?
一加Ace值得买吗?用实力诠释性能的强大