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. 【操作系统之十五】iptables黑白名单、自定义链、网络防火墙、常用动作

    1.黑白名单当链的默认策略为ACCEPT时,链中的规则对应的动作应该为DROP或者REJECT,表示只有匹配到规则的报文才会被拒绝,没有被规则匹配到的报文都会被默认接受,这就是"黑名单&qu ...

  2. ECMAScript 初探 - 基础篇

    ECMAScript 语言的标准是由 Netscape.Sun.微软.Borland 等公司基于 JavaScript 和 JScript 锤炼.定义出来的. ECMAScript 仅仅是一个描述,定 ...

  3. VUE引入jq bootstrap 之终极解决方案(测试)

    初入VUE遇见的一些问题,在网上找了些方法,再根据自己的实际项目解决的问题写得此文,,希望对你有所帮助. vue-cli快速构建项目以及引入boostrap.jq各种插件配置 vue-cli脚手架工具 ...

  4. HttpRuntime.Cache 与 HttpContext.Current.Cache

    1.HttpRuntime.Cache是应用程序级别的, 2.而HttpContext.Current.Cache是针对当前WEB上下文定义的. 3.这二个都是调用的同一个对象,不同的是:HttpRu ...

  5. Java学习:File类中的过滤器接口

    javaIO类的File类应用:过滤器接口 FilenameFilter和FileFilter都是用来过滤文件的 例如: 过滤以.jpg或者.java结尾的文件. 通过看他们的源码: 通过使用File ...

  6. Redis-1-简介与安装

    目录 1.Redis 简介 2.安装Redis 1.安装gcc redis是c语言编写的 2.下载redis安装包,在root目录下执行 3.解压redis安装包 4.进入redis目录 5.编译安装 ...

  7. 【JVM】【linux】linux上执行jmap命令查看JVM内存使用情况,报错:sun.jvm.hotspot.debugger.NoSuchSymbolException: Could not find symbol "gHotSpotVMTypes" in any of the known library name

    运行命令: jmap -heap 报错如下: Attaching to process ID , please wait... sun.jvm.hotspot.debugger.NoSuchSymbo ...

  8. Blend 多文本控件介绍

    原文:Blend 多文本控件介绍 多文本控件 RichTextBox FlowDocumentScrollViewer FlowDocumentPageViewer FlowDocumentReade ...

  9. axios FastMock 跨域 代理

    发送请求: 实现:发送请求,获取数据. 原本想自己写服务,后来无意间找到FastMock这个东东,于是就有了下文... 首先我安装了axios,在fastmock注册好了并创建了一个接口,怎么搞自行百 ...

  10. OCaml (ML家族语言)很好很强大

    理由如下: 1. 强类型,写着放心 2. 函数式. 且不是pure functional, 不用像 Haskell 那样极端.. 3. Algebric Data Types 的模式匹配 实在太爽了 ...