Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
onelogistics
leetcode
Repository navigation
Code
Issues
Pull requests
3
(3)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
leetcode
/
src
/
main
/
java
/
basic
/
doublePointer
/
HappyNumber.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
54 lines (52 loc) · 1.88 KB
master
Breadcrumbs
leetcode
/
src
/
main
/
java
/
basic
/
doublePointer
/
HappyNumber.java
Copy path
Top
File metadata and controls
Code
Blame
54 lines (52 loc) · 1.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package basic.doublePointer;
import java.util.HashSet;
import java.util.Set;
/**
* 202. Happy Number
* https://leetcode.com/problems/happy-number/
* 快乐数,给定一个正整数
* 遍历正整数的每一位数字,将其平方和累加得到新的整数,不断重复这个过程,如果最终得到的正整数为1,那么
* 就说这个数是个快乐数,如果最终陷入了无限循环,则认为这个数字不是快乐数
* Example:
* <p>
* Input: 19
* Output: true
* Explanation:
* 1^2 + 9^2 = 82
* 8^2 + 2^2 = 68
* 6^2 + 8^2 = 100
* 1^2 + 0^2 + 02 = 1
*/
public class HappyNumber {
public static void main(String[] args) {
HappyNumber happyNumber=new HappyNumber();
happyNumber.isHappy(19);
}
/**
* 这道题有一种通用解法,将历史出现过的数字存在set中,每次得到新数之后判断是否出现过,如果出现过且不为1的话,就return false。
* 另一种做法则是借鉴快慢指针的做法。
* 这个题目中一定是存在循环的。
* 如果是快乐数,则最终的循环数就是1,1经过一轮将其平方和累加得到的又是1,所以最终的循环数是1.
* 而如果不是快乐数,则肯定会在某个非1的数中出现循环
* 基于此,我们使用快慢指针来解答,如果快慢指针相等,则判断此时相遇节点的数值即可
*
* @param n
* @return
*/
public boolean isHappy(int n) {
int slow=n,fast=n;
do {
slow = findNextValue(slow);
fast = findNextValue(findNextValue(fast));
} while (slow != fast);
return fast==1;
}
private int findNextValue(int n) {
int sum = 0;
while (n > 0) {
sum += (n % 10) * (n % 10);
n = n / 10;
}
return sum;
}
}
You can’t perform that action at this time.