LeetCode_202. Happy Number
2024-08-26 22:41:53
202. Happy Number
Easy
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example:
Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
package leetcode.easy; public class HappyNumber {
public boolean isHappy(int n) {
if (n == 0) {
return false;
}
while (n != 1) {
int n2 = n;
n = 0;
while (n2 > 0) {
n += (n2 % 10) * (n2 % 10);
n2 /= 10;
}
if (n == 4) {
return false;
}
}
return true;
} @org.junit.Test
public void test() {
System.out.println(isHappy(19));
}
}
最新文章
- RecyclerView,CardView导入和使用(Demo)
- python 字符串比较
- 15套帮助你展示 App 设计的透视屏幕原型素材
- poppin_xpower_ 常城
- python分片
- PHP.8-HTML+CSS(二)-HTML详解
- C++单元测试2
- 前端开发的常用js库
- HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!
- Codeforces 803C. Maximal GCD
- H5外包 微信小程序外包 小程序外包 就找北京动点开发团队
- linux使用.net core 创建简单的MVC
- 记录一下putty的pscp的用法【转】
- jquery $.trim()去除字符串空格
- 自建k8s集群日志采集到阿里云日志服务
- linux驱动(续)
- Salesforce小知识:在简档中设置Visualforce页面的权限
- 闭包传递(floyed)
- sqlserver无法在数据库上放置锁
- Python网络爬虫之图片懒加载技术、selenium和PhantomJS