【刷题-LeetCode】202. Happy Number
- Happy Number
Write an algorithm to determine if a number n 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.
Return True if n is a happy number, and False if not.
Example:
Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
对于happy number的操作,最终的结局可能是:
- 收敛到1
- 陷入循环
- n不断增大直到无穷大
但是第三种情况肯定不会发生
| Digits | Largest | Next |
|---|---|---|
| 1 | 9 | 81 |
| 2 | 99 | 162 |
| 3 | 999 | 243 |
| 4 | 9999 | 324 |
| 13 | 9999999999999 | 1053 |
从表中可以看出,三位数最大的下一位是243,四位数最大的下一位小于999,因此最终也会小于243
解1 hashset判断是否出现环
class Solution {
public:
bool isHappy(int n) {
unordered_map<int, bool>mp;
while(!mp[n]){
mp[n] = true;
n = happy(n);
if(n == 1)return true;
}
return false;
}
int happy(int n){
int res = 0;
while(n){
int tmp = n % 10;
res += tmp * tmp;
n /= 10;
}
return res;
}
};
解2 Floyd环检测算法(龟兔赛跑)
class Solution {
public:
unordered_map<int, int>mp;
bool isHappy(int n) {
int faster = happy(happy(n)), slower = happy(n);
while(faster != 1 && faster != slower){
faster = happy(happy(faster));
slower = happy(slower);
}
return faster == 1;
}
int happy(int n){
if(mp[n])return mp[n];
int res = 0;
while(n){
int tmp = n % 10;
res += tmp * tmp;
n /= 10;
}
return mp[n] = res;
}
};
【刷题-LeetCode】202. Happy Number的更多相关文章
- 【刷题-LeetCode】200 Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【刷题-LeetCode】191 Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and return the number of '1' bits i ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- 【刷题-LeetCode】306. Additive Number
Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【刷题-LeetCode】179 Largest Number
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- Nacos——注册中心
目录 1.什么是nacos 2.使用--依赖+配置文件 3.Nacos服务分级存储模型 4.服务跨集群调用问题 5.服务集群属性--配置服务集群 6. Nacos-NacosRule负载均衡 7.根据 ...
- Linux三剑客综合练习
1.找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写 [root@localhost ~]# grep -E '^[sS]' /proc/meminfo [root@loca ...
- SpringBoot 设置请求字符串格式为UTF-8
增加一个过滤器 package com.config; import com.jetsum.business.common.constant.CharsetConstant; import lombo ...
- 1421 最大MOD值
1421 最大MOD值 基准时间限制:1 秒 空间限制:131072 KB 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 a ...
- Doing Homework(hdu)1074
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- CentOS7.6下安装Redis5.0.7
此次安装是在CentOS7下安装Redis5.0.7 一.首先准备Redis安装包 这里下载的是 redis-5.0.7.tar.gz 安装包,并将其直接放在了 root ⽬录下 压缩包下载地址:ht ...
- SpringCloud发现服务代码(EurekaClient,DiscoveryClient)
1.说明 本文介绍SpringCloud发现服务代码的开发, 通过使用EurekaClient,DiscoveryClient来发现注册中心的服务等, 从而可以自定义客户端对注册中心的高级用法. 2. ...
- Spring @Valid 和 @Validated 的区别和使用
两者区别 @Valid @Validated 标准 标准JSR-303规范 增强JSR-303规范 包 javax.validation org.springframework.validation ...
- 初识python:tkinter 实现 弹球小游戏(非面相对象)
通过 tkinter 采用非面相对象式实现弹球小游戏(使用蹩脚式面相对象实现). #!/user/bin env python # author:Simple-Sir # time:2020/8/3 ...
- JMeter_调试取样器(Debug Sampler)
大家在调试 JMeter 脚本时有没有如下几种需求: 我想知道参数化的变量取值是否正确! 我想知道正则表达式提取器(或json提取器)提取的值是否正确! 我想知道 JMeter 属性! 调试时服务器返 ...