lintcode python代码 488 快乐数
来源:互联网 发布:日本媳妇 知乎 编辑:程序博客网 时间:2024/06/05 07:10
写一个算法来判断一个数是不是”快乐数”。
一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是无限循环但始终变不到1。如果可以变为1,那么这个数就是快乐数
思路:先求各个位置上的数字 求余 得到个位数
while n != 0:
temp = n % 10
result += pow(temp, 2)
n = (n - temp) / 10
相加判断是否为1
class Solution: # @param {int} n an integer # @return {boolean} true if this is a happy number or false def isHappy(self, n): # Write your code her record = set() while True: record.add(n) n = self.Next(n) if n == 1: return True elif n in record: return False def Next(self, n): result = 0 while n != 0: temp = n % 10 result += pow(temp, 2) n = (n - temp) / 10 return result
阅读全文
0 0
- lintcode python代码 488 快乐数
- [LintCode 488] 快乐数 (Python)
- LintCode 488快乐数
- [Lintcode] #488 快乐数
- LintCode-简单-快乐数
- LintCode-快乐数
- LintCode:快乐数
- LintCode之快乐数
- lintcode,快乐数
- LintCode 快乐数
- lintcode--快乐数
- LintCode之快乐数
- lintcode:快乐数
- 快乐数-LintCode
- Lintcode快乐数问题解析
- lintcode python代码 517丑数
- LintCode 代码 翻转数
- LintCode Python 丑数II
- Python3爬虫小程序——爬取各类天气信息(2)
- 使用sinopia搭建私有npm服务
- android获取当前运行Activity名字的方法
- mapbox相关资料
- 关于react用script标签引入jsx文件的问题
- lintcode python代码 488 快乐数
- 计蒜客-加一
- iOS 转让APP,变更开发商名字 —— HERO博客
- MySQL中group_concat函数深入理解
- Coderforces Chat room
- 简单句的五大句型
- 【机器学习】python第三方模块lda包调用代码
- TensorFlow 学习资源
- A tutorial on binary descriptors – part 3 – The ORB descriptor