此文首发于我的个人博客:LeetCode 202. Happy Number–Python解法–数学题 — zhang0peter的个人博客


LeetCode题解专栏:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结
大部分题目C++和Python的解法都有。


题目地址:Happy Number - LeetCode


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:

Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

这道题目明显是数学题
Python解法如下:

class Solution:def isHappy(self, n: int) -> bool:s = set()while True:num = str(n)temp = 0for i in num:temp += int(i)**2if temp in s:return Falseelif temp == 1:return Trueelse:s.add(temp)n = temp

LeetCode 202. Happy Number--Python解法--数学题相关推荐

  1. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  2. LeetCode 41. First Missing Positive--Python 解法--数学题-找到不存在的最小正整数-O(1)空间复杂度

    题目地址:First Missing Positive - LeetCode Given an unsorted integer array, find the smallest missing po ...

  3. 【Leetcode】5 longestPalindrome python解法

    leetcode第5题 longestPalindrome 最长回文子串 python解法 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 输入: " ...

  4. LeetCode 15. 3Sum--Java,Python解法

    题目地址: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  5. leetcode 202. Happy Number

    传送门 202. Happy Number My Submissions Question Total Accepted: 56706 Total Submissions: 158441 Diffic ...

  6. python leetcode 202. Happy Number

    类似于链表环中快慢指针的思想 class Solution(object):def isHappy(self, n):""":type n: int:rtype: boo ...

  7. [LeetCode]202. Happy Number(平衡二叉树 哈希表)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. leetcode 202.Happy Number (python3 )

    题目: 题目分析:首先,本题需要判定输入的整数其各位数平方求和最终值是否为1 ?输入整数位整型,返回值为布尔型. 编程思路: 1.需要利用到循环实现每一次求和后判定是否为1 . 2.需要排除有可能出现 ...

  9. LeetCode 268. Missing Number--Python解法--数学题

    LeetCode 268. Missing Number–Python解法–数学题 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部 ...

最新文章

  1. Mybatis映射文件动态SQL语句-02
  2. C++fast power快速指数的实现(附完整源码)
  3. Java-变量、常量
  4. 3串锂电池电量检测ic bq2060pdf及应用原理图_接受电源IC需求挑战,轻松实现精确调节的电源电压!...
  5. php怎么求最小公倍数,C++_详解C语言求两个数的最大公约数及最小公倍数的方法,求两个正整数的最大公约数nbs - phpStudy...
  6. Struts项目中前端页面向后台页面传参中文出现乱码(Get请求)
  7. 编码技术新突破:字节跳动AVG让视频缩小13%
  8. java模拟时钟课程设计_Java课程设计时钟图形模拟
  9. R语言:方差分析之单因素方差分析和双因素方差分析
  10. 系统重装后恢复MySQL数据
  11. 能上QQ不能上浏览器处理方法(win11版)
  12. UG二次开发GRIP刻字
  13. excel序号自动更新
  14. 虚拟局域网软件开源_用于云和虚拟化的事实上的标准开源软件包括Linux
  15. 【单片机笔记】基于STM32的8通道的开源无线遥控器手柄
  16. buuctf MISC菜刀666
  17. ubuntu18.04 安装JLinkOB驱动以及问题解决
  18. 装机秘籍:Windows10与deepin v20双系统装机(电脑小白也能看哦!!)
  19. Vue项目JSON格式字符串和对象之间的互转
  20. foxmail中的信纸文字怎么改变颜色

热门文章

  1. 数字化如何影响药物开发
  2. iMeta期刊推特官方帐号@iMetaJournal上线
  3. PCE:南农沈其荣团队根系分泌物驱动土壤-微生物-养分之间的反馈作用以响应植物的生长...
  4. 宏基因组蚂蚁森林公益合种树项目,支持祖国绿化事业,让世界更美好
  5. EI:生物膜反应器抗性组对替加环素浓度升高的动态响应
  6. QIIME 2教程. 03老司机上路指南Experience(2020.11)
  7. 扩增子图表解读8网络图:节点OTU或类Venn比较
  8. mysql select语法_MySQL SELECT语法(一)SELECT语法详解
  9. R语言一次性读入多个csv文件实战:一次导入多个csv形成一个统一的dataframe、原生R方法、readr包、data.table
  10. R语言dataframe计算满足筛选条件的行的个数(筛选满足条件的数据行并计数):类似于excel的countif函数