「LeetCode每日一题」—— 202. 快乐数
202. 快乐数
链接:https://leetcode-cn.com/problems/happy-number/
难度:简单
题目
点击原文链接跳转查看题目
思路
今天是四月最后的一题,持续两个月的打卡活动到这里就结束了。官方选这个题也是有意为之,祝大家五一快乐。
来看这道题,快乐数的定义把位置上的数平方和,直到变成1为止。这里也说了,可能有两种情况,无限循环和变为1。
那不断的计算,看是否循环,或者为1就可以判断是否为快乐数,代码见解决方案。
方案代码
解法方案:
class Solution:
def isHappy(self, n: int) -> bool:
visited = set()
while n != 1 and n not in visited:
visited.add(n)
nxt = 0
while n != 0:
nxt += (n % 10) ** 2
n //= 10
n = nxt
return n == 1
后续计划
每日一题目录:http://flypython.com/leetcode/
相关
原创文章,作者:flypython,如若转载,请注明出处:http://flypython.com/algorithm/leetcode/362.html
相关推荐
-
「LeetCode每日一题」—— LOCF.51. 数组中的逆序对
LOCF.51. 数组中的逆序对 链接:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/难度:中等 …
24/04/2020 -
「LeetCode每日一题」—— 460. LFU缓存
460. LFU缓存 题目链接:https://leetcode-cn.com/problems/lfu-cache/难度:困难 题目 点击原文链接跳转查看题目 请你为 最不经常使…
05/04/2020 -
「LeetCode每日一题」—— 445. 两数相加 II
445. 两数相加 II 链接:https://leetcode-cn.com/problems/add-two-numbers-ii/难度:中等 题目 点击原文链接跳转查看题目 …
14/04/2020 -
「LeetCode每日一题」—— 1248. 统计「优美子数组」
1248. 统计「优美子数组」 链接:https://leetcode-cn.com/problems/count-number-of-nice-subarrays/难度:中等 题…
21/04/2020 -
「LeetCode每日一题」—— 22. 括号生成
22. 括号生成 链接:https://leetcode-cn.com/problems/generate-parentheses/难度:中等 题目 数字 n 代表生成括…
09/04/2020 -
「LeetCode每日一题」—— 887. 鸡蛋掉落
887. 鸡蛋掉落 链接:https://leetcode-cn.com/problems/super-egg-drop/难度:难度 题目 你将获得 K 个鸡蛋…
11/04/2020 -
「LeetCode每日一题」——1160. 拼写单词
1160. 拼写单词 链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters…
17/03/2020 -
「LeetCode每日一题」——LCOF.57 – II. 和为s的连续正数序列
面试题57 – II. 和为s的连续正数序列 链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu…
06/03/2020 -
「LeetCode每日一题」—— LCOF.56 – I. 数组中数字出现的次数
LCOF.56 – I. 数组中数字出现的次数 链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-d…
28/04/2020 -
「LeetCode每日一题」——169. 多数元素
169. 多数元素 链接:https://leetcode-cn.com/problems/majority-element/难度:简单 题目 给定一个大小为 n 的数组,找到其中…
13/03/2020
您必须登录才能发表评论。