leetcode 202. Happy Number 判断一个数是否是“Happy Number” ---------- java

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: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

按照上述规则,如果能够满足上述规则得数那么就是一个Happy Number

用一个set来记录已经出现过得数,避免死循环

public class Solution {

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet();
        return isHappyNum(n, set);
    }

    public boolean isHappyNum(int n, Set set){
        if (set.contains(n)){
            return false;
        }
        set.add(n);
        int num = 0;
        while (n != 0){
            num += (n % 10) * (n % 10);
            n /= 10;
        }
        if (num == 1){
            return true;
        } else {
            return isHappyNum(num, set);
        }
    }
}

2、使用O(1)的空间复杂度。很巧妙,如果会出现循环的情况,那么x和y一定会相遇,也就一定会跳出循环。

public class Solution {
    public boolean isHappy(int n) {
        int x = n;
        int y = n;
        while(x>1){
            x = cal(x) ;
            if(x==1) return true ;
            y = cal(cal(y));
            if(y==1) return true ;

            if(x==y) return false;
        }
        return true ;
    }
    public int cal(int n){
        int x = n;
        int s = 0;
        while(x>0){
            s = s+(x%10)*(x%10);
            x = x/10;
        }
        return s ;
    }
}
时间: 2024-10-20 16:42:41

leetcode 202. Happy Number 判断一个数是否是“Happy Number” ---------- java的相关文章

40. leetcode 202. Happy Number

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

[LeetCode] Power of Two 判断2的次方数

Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in O(1) time and using O(1) space? 这道题让我们判断一个数是否为2的次方数,而且要求时间和空间复杂度都为常数,那么对于这种玩数字的题,我们应该首先考虑位操作 Bit Operation.在LeetCode中,位操作的题有很多,比如比如Repeated DNA Seque

[LeetCode] Power of Three 判断3的次方数

Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases. 这道题让我们判断一个数是不是3的次方数,在Le

Java杂谈之二----怎样判断一个数是水仙花数以及穷举水仙花数

首先明确一下什么是水仙花数 百度说,水仙花数指一个n位数(n>=3),它的每个位上的数字的n次幂之和等于它本身 例如:1^3+5^3+3^3=153 水仙花数只是自幂数的一种,严格来说三位数的3次幂数才能成为水仙花数. 但其实也分一位自幂数,两位自幂数,三位自幂数,四位自幂数等等. 所以鉴于水仙花数的定义的不确定和模糊性 以下代码示例不仅限于三位数的水仙花数,主要涉及的是思想问题. 类名:JavaNarcissus 构造函数:JavaNarcissus() 判断一个数是否为水仙花数:IsNarc

(hdu step 2.1.2)How many prime numbers(判断一个数是否是质数)

题目: How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8513 Accepted Submission(s): 2716   Problem Description Give you a lot of positive integers, just to find out how many pr

判断一个数是否是质数

质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,能被整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数. 如何判断一个数是否是质数: 代码1: 1 /** 2 * 判断给定的数字是否为素数(质数) 3 * @param num 4 * @return 5 */ 6 public static boolean isPrime(int num){ 7 if(num < 2){ 8 return false; 9 } 1

&运算符运用之判断一个数是不是2^n

#include <stdio.h>int main(){ int n; printf("please enter a number:"); scanf("%d",&n); if(n&(n-1)==0) {  printf("yes\n");   } else {  printf("no\n");  } return 0;} &运算符运用之判断一个数是不是2^n

判断一个数的n进制是不是回文数

#include<stdio.h> int circle(int m,int n)//该函数判断m的n进制数是否为回文数 { int s=0,l=m; while(l) { s=s*n+l%n;//该语句是反顺序计算数值 l/=n; } return s==m;//如果是回文数,那么正反顺序的数字应该是相等的 } void main() { int num[]={434,783,909}; int sys[]={2,8,10,16}; int i,j; for(i=0;i<3;i++)

[LeetCode] Power of Four 判断4的次方数

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? Credits:Special thanks to @yukuairoy fo