202. Happy Number [easy] (Python)
来源:互联网 发布:java遍历json对象 编辑:程序博客网 时间:2024/06/08 11:14
题目链接
https://leetcode.com/problems/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: 19 is a happy number
12+92=82
82+22=68
62+82=100
12+02+02=1
题目翻译
写一个程序来判断一个数字是否是“happy”的。
所谓 happy number,是被这样的过程定义的数字:初始时有一个正整数,用该整数每位数字的平方之和代替这个整数,重复该过程直至数字变为1(之后不再变化),或者陷入一个无尽的循环但该循环不包括1。那些能终止于1的数字就称为 happy number。
比如:19就是一个 happy number,因为
思路方法
思路一
按照“happy number”的定义,直接循环计算各位平方和,观察是否收敛到1,若是则是 happy number。为了判断循环是否开始重复,要用一个字典(dict)或集合(set)来保存已经出现的数字,dict的效率更高。
代码一
class Solution(object): def isHappy(self, n): """ :type n: int :rtype: bool """ num_dict = {} while True: num_dict[n] = True sum = 0 while(n>0): sum += (n%10)*(n%10) n /= 10 if sum == 1: return True elif sum in num_dict: return False else: n = sum
通过分析该问题,利用一些分析结果,可以对算法进行优化。比如利用10以内的happy number只有1和7,或者先求出100以内的所有happy number等。
代码二
class Solution(object): def isHappy(self, n): """ :type n: int :rtype: bool """ happySet = set([1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97]) while n>99: n = sum([int(x) * int(x) for x in list(str(n))]) return n in happySet
思路二
上面的思路判断是否出现循环可能用到大量的额外空间,如果想要压缩空间复杂度到 O(1),可以考虑用弗洛伊德环检测算法(Floyd Cycle Detection Algorithm),代码如下。
代码
class Solution(object): def isHappy(self, n): """ :type n: int :rtype: bool """ slow = fast = n while True: slow = self.squareSum(slow) fast = self.squareSum(fast) fast = self.squareSum(fast) if slow == fast: break return slow == 1 def squareSum(self, n): sum = 0 while(n>0): tmp = n % 10 sum += tmp * tmp n /= 10 return sum
说明
关于该方法的一些解释,可以参考:
https://leetcode.com/discuss/71625/explanation-those-posted-algorithms-mathematically-valid
PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51315486
- 202. Happy Number [easy] (Python)
- 202. Happy Number (Easy)
- 202. Happy Number 难度:easy
- LeetCode 202. Happy Number (Easy)
- Easy-题目20:202. Happy Number
- Leetcode 202. Happy Number (Easy) (cpp)
- 202. Happy Number Leetcode Python
- [leetcode: Python]202.Happy Number
- [Leetcode 202, Easy] Happy Number
- 【LeetCode】(202)Happy Number(Easy)
- <LeetCode><Easy> 203 Happy Number
- Leet Code OJ 202. Happy Number [Difficulty: Easy]
- 202.leetcode Happy Number(easy)[数学 快乐数]
- 【Leetcode】202-Happy Number【Java实现】【Easy】
- 202. Happy Number
- [LeetCode]202.Happy Number
- 【LeetCode】202.happy number
- LeetCode 202. Happy Number
- block传值
- LINUX/UNIX的文件操作函数
- HDU - 1556 Color the ball
- 贪婪的你
- oc(1)
- 202. Happy Number [easy] (Python)
- 前端开发常用的css样式
- 初学java编程简单双色球产生器,附代码截图
- Oracle INDEX FAST FULL SCAN与 INDEX FULL SCAN
- JavaMail系列(二) JavaMail API简介和配置开发环境
- POJ 1122 FDNY to the Rescue!(最短路径)
- 用FileInputStream和FileOutPutStream读写文件
- bzoj 3333: 排队计划(树状数组+线段树)
- javaweb学习总结(二十三)——jsp自定义标签开发入门