「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每日一题」—— 11. 盛最多水的容器
11. 盛最多水的容器 链接:https://leetcode-cn.com/problems/container-with-most-water/难度:中等 题目 思路 这道题和…
18/04/2020 -
「LeetCode每日一题」——169. 多数元素
169. 多数元素 链接:https://leetcode-cn.com/problems/majority-element/难度:简单 题目 给定一个大小为 n 的数组,找到其中…
13/03/2020 -
「LeetCode每日一题」——LCCI.17.16. 按摩师
LCCI.17.16. 按摩师 链接:https://leetcode-cn.com/problems/the-masseuse-lcci/难度:简单 题目 一个有名的按摩师会收到…
24/03/2020 -
「LeetCode每日一题」—— 1248. 统计「优美子数组」
1248. 统计「优美子数组」 链接:https://leetcode-cn.com/problems/count-number-of-nice-subarrays/难度:中等 题…
21/04/2020 -
「LeetCode每日一题」—— 72. 编辑距离
72. 编辑距离 题目链接:https://leetcode-cn.com/problems/edit-distance/难度:困难 题目 给你两个单词 word1 和&…
06/04/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每日一题」——876. 链表的中间结点
876. 链表的中间结点 链接:https://leetcode-cn.com/problems/middle-of-the-linked-list/难度:简单 题目 给定一个带有…
23/03/2020 -
「LeetCode每日一题」——836. 矩形重叠
836. 矩形重叠 链接:https://leetcode-cn.com/problems/rectangle-overlap/难度:简单 题目 矩形以列表 [x1, y1, x2…
18/03/2020 -
「LeetCode每日一题」225. 用队列实现栈
LeetCode每日一题 周五跟大家预告了LeetCode每日一题的活动,今天活动已经开始了。打开leetcode中文版,你可以在题库中看到制定的题目,行动起来吧。 在这里帖下打卡…
01/03/2020 -
「LeetCode每日一题」—— LCCI.08.11. 硬币
LCCI.08.11. 硬币 链接:https://leetcode-cn.com/problems/coin-lcci/难度:中等 题目 点击原文链接跳转查看题目 思路 这题是完…
23/04/2020
您必须登录才能发表评论。