Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package leetcode;
- import java.util.HashMap;
- import java.util.HashSet;
- public class HappyNumber202 {
- public static void main(String[] args) {
- int n =2;
- System.out.println(isHappy(n));
- }
- private static boolean isHappy(int n) {
- HashSet<Integer> set = new HashSet<>();
- //int max=n;
- //int total_sum = findDigit(n);
- while (n!=1) {
- if(set.contains(n)){
- return false;
- }
- set.add(n);
- n = findDigit(n);
- }
- return true;
- }
- static int findDigit(int n){
- int sum=0;
- while (n>0){
- int digit = n%10;
- sum += doSquare(digit);
- n=n/10;
- }
- return sum;
- }
- static int doSquare(int i) {
- int sum= i*i;
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement