当前位置:网站首页>快乐数~~~(其实我一点都不快乐) && 丑数
快乐数~~~(其实我一点都不快乐) && 丑数
2022-07-21 17:46:00 【忱叁】
hashmap解法也可以(就是把前面的结果进行存储,然后去判断这个结果在前面是否出现过 ,如果出现过肯定就是进入了循环,此时直接判定)否则就是没出现,就是判定为快乐数
这是快慢指针解法,通过快慢指针,进去·循环说明(此时没有出现1,且他们两个指针未相遇,那么我们就让快慢指针去走两步和一步,除了循环,直接去判断是否是因为出现了1才出的循环即可)
class Solution {
public int getnext(int n){
int sum = 0;
int ret = 0;
while(n>0){
ret = n%10;
n = n/10;
sum += ret*ret;
}
return sum;
}
public boolean isHappy(int n) {
int fastrunner = getnext(n);
int slowrunner = n;
while(fastrunner != 1 && slowrunner != fastrunner){
slowrunner = getnext(slowrunner);
fastrunner = getnext(getnext(fastrunner));
}
return fastrunner == 1;
}
}
class Solution {
public boolean isUgly(int n) {
//如果这个数= 2^n *3或者2^n *5 3^n *2或者3^n *5 5^n *2或者5^n *3
if(n < 0){
return false;
}
if(n == 0){
return false;
}
int[] factors = {2,3,5};
for(int factor : factors){
while(n % factor == 0){
n/=factor;
}
}
return n==1;
}
}
边栏推荐
- QT配置OpenCV(二):成功
- 类加载器简介
- Pipes, redirects, and regular expressions
- NodeJS使用Express框架进行POST请求报“BadRequestError:request aborted”
- String str = new String(“abc“) 到底创建了几个变量?
- 单细胞论文记录(part18)--Spectral clustering based on learning similarity matrix
- ES版i9-13900K亮相CPU-Z数据库 跑分数据过于谦虚
- 迷你考试-考试系统
- Writing of ultrasonic ranging driver based on [Samsung 6818] chip
- "Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
猜你喜欢
CopyOnWriteArrayList 浅析
Token
LeetCode703:数据流中的第 K 大元素
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
使用OpenGL纹理数组实现高精度实时Lut滤镜
OpenGL贴图保持原比例的一种方法
position: -webkit-sticky; /* for Safari */ position: sticky;
Configure opencv in QT
好轮子收藏:一个支持几乎所有流行格式的图像加载库stb_image.h
类加载器简介
随机推荐
QT中配置OpenCV
cocoapod安装问题
About the plaintext syntax of vditor Publishing
Leetcode-468: verify IP address
Openlayers uses canvas to draw round avatar icons
并发编程-----------集合
Use style to solve the white border when the irregular screen is horizontal
Ctrip spark multi tenant query service evolution, Apache Kyuubi can be expected in the future
QT配置OpenCV(二):成功
Visual Studio的intellisense: '没有可用的成员'
Pipes, redirects, and regular expressions
【OAuth2】二、OAuth2.1的已知变动
My ideal "meta universe"
Shortcut keys for command line editing and operation skills commands
MySQL的一些笔记
QT configuration opencv (II): successful
OpenGL:freeglut ERROR: Function <glutCreateWindow> called without first calling ‘glutInit‘.
华泰证券渠道开户安全吗,怎么看是不是正规
单细胞论文记录(part17)--Integrating sc transcriptomic data across different conditions, tech and species
Custom picture crop box