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. 刷完欧拉计划中难度系数为5%的所有63道题,我学会了Rust中的哪些知识点?

    我为什么学Rust? 2019年6月18日,Facebook发布了数字货币Libra的技术白皮书,我也第一时间体验了一下它的智能合约编程语言MOVE,发现这个MOVE是用Rust编写的,看来想准确理解 ...

  2. 《Linux就该这么学》培训笔记_ch02_一些必须掌握的Linux命令

    本文在原来作者的基础上做一些符合自己的修改.原文参考: <Linux就该这么学>培训笔记_ch02_一些必须掌握的Linux命令.     本章的内容虽然多,基本都是书本原话,但是笔记能精 ...

  3. SpringBoot第八篇:整合MyBatis-Generator

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10894278.html 版权声明:本文为博主原创文章,转载请附上博文链接! 注意:本章有大量代码 ...

  4. pandas的使用(4)

    pandas的使用(4)--文件读取和保存

  5. | C语言I作业12

    C语言I作业12-学期总结 标签:18软件 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/S ...

  6. 网络基础-------------给电脑设置IP

    ip 是每一台电脑进入互联网的一个必备钥匙,没有它就不能体会冲浪的乐趣,当我们使用电脑连接无线时我们就会被自动分配一个ip地址(DHCP),这样我们就可以凭借这个IPV4地址来进行冲浪了,但是自动分配 ...

  7. Create GUID / UUID in JavaScript?

    Code function uuidv4() { return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.g ...

  8. tomcat 下 base64图片上传超过2m的解决方案

    方案一: tomcat部署下默认post请求提交参数大小为2M左右,超过这个大小,就会传值不成功 要使post请求参数无大小限制,需要在server.xml上修改,如下: <Connector ...

  9. jetbrain 公司2019年全套产品的破解方案

      百度网盘下载地址是:链接:https://pan.baidu.com/s/1E4E76Oglfexed0iHNiXjEQ  密码:pehx     ======================== ...

  10. VMWare 下安装 Windows XP

    准备工作 安装VMware 我这里安装的是 VMware-workstation-full-12.1.1-3770994.exe 下载Windows XP with SP3 从网上下载的MSDN版本X ...