【刷题-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 " => ...
随机推荐
- LuoguP3932 浮游大陆的68号岛 题解
Content 在一个无限长的数轴上有 \(n\) 个点.第 \(i\) 个点上面有 \(a_i\) 件物品,且第 \(i\) 个点到第 \(i+1\) 个点的距离为 \(b_i\). 定义从第 \( ...
- LuoguB2045 晶晶赴约会 题解
Content 贝贝邀请晶晶下个星期 \(x\)(如果 \(x=7\) 表示星期日)一起去看展览,但是晶晶下个星期 \(1,3,5\) 都有课.请你判断晶晶能否同意贝贝的请求. 数据范围:\(x\in ...
- LuoguP4419 [COCI2017-2018#1] Cezar 题解
Content 有一个牌库,有一些点数为 \(1\sim 11\) 的牌,其中除了点数为 \(10\) 的牌有 \(16\) 张之外,其余点数的牌各有四张.现在玩一个游戏,已经拿出了 \(n\) 张牌 ...
- mpstat 查看多核CPU负载状态
mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有 ...
- 海康威视Java SDK拉流(一)初始化SDK
19年的时候做了一个视频分析的产品,用户使用的安防摄像机基本的都是海康大华宇视,今天写一下关于Java调用海康威视摄像机的demo,当时也踩了很多坑.写个博客记录一下 测试环境: 系统:Centos ...
- nim_duilib之msgbox用法(23)
概述 本文将介绍 msgbox 的用法 更多用法,请参考 源码 改进了原有的xml样式 一个样式 xml结构 整体垂直布局 xml源码 demo源码下的msg/msg.xml文件内容 改为如下 注意: ...
- 再谈多线程模型之生产者消费者(基础概念)(c++11实现)
0.关于 为缩短篇幅,本系列记录如下: 再谈多线程模型之生产者消费者(基础概念)(c++11实现)[本文] 再谈多线程模型之生产者消费者(单一生产者和单一消费者)(c++11实现) 再谈多线程模型之生 ...
- Java不可变类与final类
概念 Java的不可变类是指八个基础类型的包装类和String,他们的数据成员是不可变的.使用加法等操作时,其实是创建了一个新的对象. Java的final类是对类用关键字final进行修饰,说明该类 ...
- Linux磁盘分区与挂载
原理介绍 在Linux世界中,一切皆目录,每一块硬盘分区对应Linux的一个目录,所以我们可以通过管理目录来管理硬盘分区,而将硬盘分区与文件目录关联的操作就成为"挂载"[mount ...
- Java Web程序设计笔记 • 【第1章 Web应用程序】
全部章节 >>>> 本章目录 1.1 Web 应用程序 1.1.1 Web 应用程序概述 1.1.2 Web 应用程序的工作原理 1.1.3 实践练习 1.2 HTTP协议 ...