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:

Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100

Handle the number directly, common way is thougth % or / operator

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
let memo = {}; while(n !== && !memo[n]) {
memo[n] = true;
let sum = ;
while(n > ) {
sum += (n % ) *(n % );
n = Math.floor(n / );
}
n = sum;
} return n == ;
};

Second way:

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
return helper(n, {});
}; function helper(n, memo) {
const ary = `${n}`.split('');
let sum = ;
for (let n of ary) {
sum += Math.pow(parseInt(n, ), );
} if (sum === ) {
return true;
} if (sum in memo) {
return false;
}
memo[sum] = true;
return helper(sum, memo);
}

[Algorithm] 202. Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  3. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  4. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. 【LeetCode】202 - Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  8. 202. Happy Number

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  9. LeetCode OJ 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

随机推荐

  1. Servlet的Listener介绍

    当Web应用在Web容器中运行时,Web应用内部会不断地发生各种事件:如Web应用被启动.Web应用被停止.用户session开始.用户session结束等.通常这些Web操作对开发者是透明的.但Se ...

  2. 调用其他python脚本文件里面的类和方法

    问题描述: 自己编写了若干个Python脚本. 在testC.py里面需要调用testA.py和testB.py里面的若干类和方法.要怎么办? 需要都打包.安装,再去调用吗? 其实不必那么麻烦. 这里 ...

  3. CyclicBarrier一组线程相互等待

    /** * CyclicBarrier 一组线程相互等待 */ public class Beer { public static void main(String[] args) { final ; ...

  4. 第五节:EF Core中的三类事务(SaveChanges、DbContextTransaction、TransactionScope)

    一. 说明 EF版本的事务介绍详见: 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges.DBContextTransaction.TransactionScope). 本节主 ...

  5. C# LDAP认证登录类参考

    public class LDAPHelper     {         private DirectoryEntry _objDirectoryEntry;         /// <sum ...

  6. vs2017 项目生成时不产生xml文件的方法

    在项目.csproj文件 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' & ...

  7. json时间格式化

    //格式化日期字符串 String.prototype.jsonDateFormat = function (format) { var date, timestamp, dtObj timestam ...

  8. spring-security.xml——安全性框架配置文件

    <?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://w ...

  9. 看一下“Dubbo 2.7”的三大新特性

    Dubbo 2.7.x 作为 Apache 的孵化版本,除了代码优化之外,还新增了许多重磅的新特性,本文将会介绍其中最典型的三个新特性: 一.异步化改造 二.三大中心改造 三.服务治理增强 一.异步支 ...

  10. FCC-学习笔记 DNA Pairing

    FCC-学习笔记  DNA Pairing 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp.cn/;英 ...