1. 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的更多相关文章

  1. 【刷题-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. ...

  2. 【刷题-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 ...

  3. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  4. LeetCode刷题------------------------------LeetCode使用介绍

    临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...

  5. 【刷题-LeetCode】306. Additive Number

    Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...

  6. 【刷题-LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  7. 【刷题-LeetCode】179 Largest Number

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  8. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. 【leetcode刷题笔记】Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

随机推荐

  1. SP16033 TIPTOP - Tip Top Game 题解

    Description Alim 和 Sufian 是好朋友.他们最近找到了一个好玩的游戏,叫做 Tip Top.游戏规则如下: 确定一个整数. 找出这个整数的所有因子. Alim 先手,每人轮流取一 ...

  2. CF1099A Snowball 题解

    Content 有一个重量为 \(w\) 的雪球从高度为 \(h\) 的位置开始滚落,每秒它的高度会减少 \(1\),同时在高度为 \(i\) 的位置它的重量会增加 \(i\).在雪球滚动的路线上还有 ...

  3. org.apache.jasper.runtime.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl

    org.apache.jasper.runtime.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl错误怎么解决: ...

  4. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 递归 循环 日期 题目描述 Given an integer ...

  6. 【九度OJ】题目1065:输出梯形 解题报告

    [九度OJ]题目1065:输出梯形 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1065 题目描述: 每组测试 ...

  7. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  8. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  9. 「算法笔记」2-SAT 问题

    一.定义 k-SAT(Satisfiability)问题的形式如下: 有 \(n\) 个 01 变量 \(x_1,x_2,\cdots,x_n\),另有 \(m\) 个变量取值需要满足的限制. 每个限 ...

  10. Mind the Box: $\ell_1$-APGD for Sparse Adversarial Attacks on Image Classifiers

    目录 概 主要内容 Croce F. and Hein M. Mind the box: \(\ell_1\)-APGD for sparse adversarial attacks on image ...