当前位置:网站首页>Leetcode question brushing: hash table 03 (happy number)
Leetcode question brushing: hash table 03 (happy number)
2022-06-23 18:25:00 【Taotao can't learn English】
Write an algorithm to judge a number n Is it a happy number .
「 Happy number 」 Defined as : For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions , Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1. If It can be 1, So this number is the happy number .
If n If it's happy, count it back True ; No , Then return to False .
Example :
Input :19
Output :true
explain :
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
package com.programmercarl.hashtable;
import java.util.HashSet;
/** * @ClassName IsHappy * @Descriotion https://leetcode.cn/problems/happy-number/ * @Author nitaotao * @Date 2022/6/20 16:14 * @Version 1.0 * Happy number **/
public class IsHappy {
public static void main(String[] args) {
System.out.println(isHappy(19));
}
public static boolean isHappy(int n) {
/** * Each calculated result is added to set aggregate , If there is repetition , Then it falls into a cycle , Is unhappy */
HashSet<Integer> set = new HashSet<>();
// If this number doesn't appear
while (!set.contains(n)) {
// Record this number
set.add(n);
int sum = 0;
while (n / 10 != 0) {
sum += (n % 10) * (n % 10);
n = n / 10;
}
sum += n * n;
if (sum == 1) {
return true;
}
n = sum;
}
// If there has been , It enters the cycle
return false;
}
}

To tell you the truth, I had no idea at first , Then I looked at the solution , A word awakens the dreamer .
Ideas
The subject looks like a mathematical problem , It's not !
There is a meeting in the title Infinite loop , So in other words In the process of summation ,sum It's going to repeat itself , It's very important to solve the problem !
So this topic uses the Hashi method , To judge this sum Whether it occurs repeatedly , If it's repeated, that's it return false, Otherwise, keep finding sum by 1 until .
Welcome to accompany me Brush problem
边栏推荐
- Landing of global organizational structure control
- 深入理解 padding
- Redis 集群
- Goframe framework: add tracing Middleware
- 云原生行业应用崛起,从“可用”到“好用”有多远?
- 【華中科技大學】考研初試複試資料分享
- [binary tree] flip the binary tree to match the preorder traversal
- uniapp项目中防止用户重复提交
- The battlefield of live broadcast e-commerce is not in the live broadcast room
- 聊一聊数据库的行存与列存
猜你喜欢
![[esp8266-01s] get weather, city, Beijing time](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266-01s] get weather, city, Beijing time

README

QT based graphics rendering system documentation + project source code and executable exe files + system instructions
![[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing

Will programmers become very professional in the future?

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design

What does the science and technology interactive sand table gain popularity by virtue of

Wechat applet startlocationupdatebackground() simply realizes rider distribution location

客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...

【win10 VS2019 opencv4.6 配置参考】
随机推荐
深入理解 padding
[sword finger offer] 45 Arrange the array into the smallest number
leetcode刷题:哈希表03 (快乐数)
云原生行业应用崛起,从“可用”到“好用”有多远?
[tool C] - lattice simulation test 2
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
知道创宇:内容向善,AI+人工赋能
Latex compiled successfully but could not be output to PDF
Which securities company is good for opening a mobile account? Is online account opening safe?
Redis Cluster
一,数组--滑动窗口问题--长度最小的子数组--水果成篮问题
[sword finger offer] 46 Translate numbers into strings
Dive Into Deep Learning——1、前言
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
『忘了再学』Shell流程控制 — 39、特殊流程控制语句
Paper reading (47):dtfd-mil: double tier feature interpretation multiple instance learning for histopathology
Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
How to solve the problem that the esp8266-01s cannot connect to Huawei routers
Asynchronous or thread pool
【華中科技大學】考研初試複試資料分享