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. java OutOfMemorry

    首先需要明确OOM并不一定会导致程序挂掉,导致服务不可用的是堆内存被耗尽,从而使得主线程直接退出,或者所有工作线程频繁因为OOM异常终止,java分配数组会直接消耗内存,一个对象引用会占用四个字节. ...

  2. Golang(十一)TLS 相关知识(二)OpenSSL 生成证书

    0. 前言 接前一篇文章,上篇文章我们介绍了数字签名.数字证书等基本概念和原理 本篇我们尝试自己生成证书 参考文献:TLS完全指南(二):OpenSSL操作指南 1. OpenSSL 简介 OpenS ...

  3. linux写shell注意的问题

    linux写shell注意的问题一定要vi crontab.sh来写 ps:在windows系统中编辑过这个文件,就会出现类似的换行符 这样导致linux系统中运行sh报错 比如会出现$MQ字符 如果 ...

  4. 【01】Saltstack:从零开始 Saltstack

    写在前面的话 最近一直都在整理以前的乱七八糟的笔记,所以会有很多老旧的东西都会被拉出来重新遛遛.算是再度系统的进行学习. 关于 Saltstack 的一些概念 Saltstack 是基于 Python ...

  5. C#左移运算符

     << 左移操作符.简单理解是对变量进行与2的n次乘方的运算.比如x<<1=x*2x<<2=x*4x<<3=x*8x<<4=x*16依此类推 ...

  6. wsl中的git问题

    当使用wsl打开Windows下的仓库时可能会出现所有文件都被标记为modified,这时一般有两种情况. 文件权限问题 由于wsl申请对文件的读写权限导致文件的权限发生改变.这时只需修改git的设置 ...

  7. 【JZOJ】2126. 最大约数和

    题目大意 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 分析 把我们分解出来的因数进行合并,存在一个不知名的数组里,然后我们大可开始我们的迪屁!!(bag),我们可以 ...

  8. WampServer出现You don’t have permission to access/on this server提示

    WampServer出现You don’t have permission to access/on this server提示 本地搭建WampServer,输入http://127.0.0.1访问 ...

  9. .NET Core 下调用WebAPI

    前言 今天我们介绍多种客户端调用WebApi的方式,可以是原生写的,也可以借助.NET 框架下的其他HTTP库.我们一起来看看它们之间的一些异同吧- RestSharp 首先要介绍的就是这款REST ...

  10. sql的日期格式化转化

    1. DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. DATE_FORMAT(date,format) 可以使用的格式有: 格式 描述 %a 缩写星期名 %b 缩写月名 %c 月 ...