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 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
  • 【算法思路】:根据Tag提示,可能要用到哈希表和数学常用库。哈希表用来查重,看计算的时候是否存在循环:若出现循环,直z接判断是非快乐数,返回false;没有的话,放在哈希表,用来后面的查重。while(ln!=0) { sum += Math.pow(n%10, 2); n /= 10;} 计算一个数的平方,Math.pow(a,b),a的b次方,比 n = a*a+b*b+c*c更优很多,不受几位数的限制,使用起来方便实用。</p><p></p><p>java代码:</p><pre name="code" class="java">
  • public class Solution {public boolean isHappy(int n) {
            if(n<=0)
                return false;  
            long ln = n;  
           // 同计算循环小数一样, 如果出现循环, 则无需继续计算,直接返回false即可. 
            Set<Long> set = new HashSet<Long>();  
              
            while(ln<=Integer.MAX_VALUE) {  
                if(set.contains(ln) ) 
                    return false; //有循环,直接判断是非快乐数
                else set.add(ln);  //没有,则添加到哈希表里来,用来后面的查重
                ln = digitSquare(ln);  
                if(ln == 1) 
                    return true;  
            }  
            return false;  
        }  
          
        private long digitSquare(long n) { 
            long sum = 0;  
            while(n!=0) {  
                sum += Math.pow(n%10, 2);  //计算一个数的平方,比 n = a*a+b*b+c*c更优很多
                n /= 10;  
            }  
            return sum;  
        }  
    }

    Testcase:

    n=1是快乐数,n = 2-6不是快乐数;

    当n = 2,2*2=4,4*4=16,1*1+6*6=37,3*3+7*7=58,5*5+8*8=89,8*8+9*9=145,1*1+4*4+5*5=42,4*4+2*2=20,2*2+0=4;出现了循环;

    当n = 3,3*3= 9,9*9=81,8*8+1*1=65,6*6+5*5=61,6*6+1*1=37,3*3+7*7=58,5*5+8*8=89,8*8+9*9=145,1*1+4*4+5*5=42——20——4,会进入n=2时的循环当中

    ……

    当n=6,6*6= 36,3*3+6*6=45,4*4+5*5=41,4*4+1*1=17,1*1+7*7=50——25——29——85——89——145——42——20——4,又进入到n=2的循环中;

    当n=7,7*7=49,4*4+9*9=97——130——10——1

    当n= 8,8*8=64——52——29——85——89——145——42——20——4,又进入到n=2的循环中;

    当n =9,9*9=81——65——61——37——58——89——145——42——20——4,

    综上所述,归纳得n = 2-6不是快乐数;所有的非快乐数,非快乐数都是通过最后n=4,在2~6之间进入循环的。


    一种更比较巧妙地解法,利用 数学归纳法原理。由于非快乐数都是通过n=4进入循环,所以代码也可以:

    public class Solution {
        public boolean isHappy(int n) {
           
            if(n == 1)
              return true;
              
            else if(n>=2&&n<=6)
              return false;
              
             while(n>6){  
                int next = 0;  
                while(n!=0){
                  next+=(n%10)*(n%10); 
                  n/=10;
                }  
                n = next;  
            }  
            return n==1;  
        }  
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值