[Algorithm] 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:
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的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- 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' ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【LeetCode】202 - Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- 202. Happy Number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...
- LeetCode OJ 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- Oracle 自定义函数实现列转行效果
在 Oracle 领域,我相信一说到列转行大部分人都会立马想到 WM_CONCAT 函数,我觉得主要是因为该函数比较实用.但事实上 WM_CONCAT 并非官方公开函数,使用会存在一定的风险:函数返回 ...
- phpredis可视化工具RedisDesktopManager
phpredis可视化工具RedisDesktopManager这个管理phpredis还不错下载地址https://redisdesktop.com/download刷新数据可以右键db0 relo ...
- ReentrantLock 锁释放源码分析
ReentrantLock 锁释放源码分析: 调用的是unlock 的方法: public void unlock() { sync.release(1); } 接下来分析release() 方法: ...
- 使用docker安装gitlab,两台电脑gitlab库相互迁移
原文来自合伙呀 https://hehuoya.com/2019/09/30/gitlab-docker/ Docker for gitlab brew cask install docker do ...
- nodejs anywhere 搭建本地静态文件服务
一.背景 工作中有时候往往会遇到下述场景:例如需要将新打好的安装包等文件临时性的给到同事,可能还需要给到多个同事.这时,我们往往有如下几种方案: 1,一般都会有公司内部的文件系统,上传文件后将对应的地 ...
- vue router 常用操作
1. 普通路由 const routes = [ { path: '/index', component: index } ] 2. 重定向 redirect const routes = [ { ...
- Prometheus Grafana可视化展示Linux资源使用率
Prometheus Grafana可视化展示Linux资源使用率 Grfana官方仪表盘下载:https://grafana.com/dashboards 数据源推荐:https://grafan ...
- Kubernetes增强型调度器Volcano算法分析【华为云技术分享】
[摘要] Volcano 是基于 Kubernetes 的批处理系统,源自于华为云开源出来的.Volcano 方便 AI.大数据.基因.渲染等诸多行业通用计算框架接入,提供高性能任务调度引擎,高性能异 ...
- 2.Shell脚本中的set指令,比如set -x 和 set -e
set参数介绍 set指令能设置所使用shell的执行方式,可依照不同的需求来做设置 -a 标示已修改的变量,以供输出至环境变量. -b 使被中止的后台程序立刻回报执行状态. -C 转向所产生的文件无 ...
- open live writer安装以及代码高亮、折叠插件安装
一.目的 方便在本地写博客,不用在浏览器上写. 二.open live writer的安装 下载open live writer 这是我的 链接:https://pan.baidu.com/s/1u8 ...