题目地址: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】5 longestPalindrome python解法

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

  3. 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? ...

  4. leetcode 202. Happy Number

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

  5. python leetcode 202. Happy Number

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

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

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

  7. leetcode 202.Happy Number (python3 )

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

  8. LeetCode 202. Happy Number--Python解法--数学题

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

  9. LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法

    LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...

  10. LeetCode 264. Ugly Number II--C++,Python解法

    题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...

最新文章

  1. 如何做EL表达式能调用的函数-小例子(转)
  2. 这个德国山寨工厂靠抄袭干到240亿,让硅谷恨之入骨
  3. 吴恩达深度学习课程deeplearning.ai课程作业:Class 2 Week 3 TensorFlow Tutorial
  4. 转: centos7 安装 juypter notebook 教程
  5. 传统APP与微信端APP十大优劣对比
  6. Leetcode 17.电话号码的组合(回溯法)
  7. 触发器及其应用实验报告总结_2020年中考总复习: 光现象、透镜及其应用知识点总结...
  8. liunx 之 redHat 下 java 环境的配置和安装
  9. 【SpringBoot_ANNOTATIONS】组件注册 03 FilterType
  10. 物流管理源代码java_基于jsp的物流管理-JavaEE实现物流管理 - java项目源码
  11. codeforces 558D Guess Your Way Out! II 规律
  12. iOS10 适配汇总
  13. 表示自己从头开始的句子_表示一切从头开始的唯美句子38条
  14. 华为mate7android版本,华为Mate7升级安卓6.0(EMUI4.0)M版本详细图文教程
  15. NET 2.0(C#)调用ffmpeg处理视频的方法
  16. 董事长和总经理的最大区别
  17. Windows Server 2008 简体中文 正式版 下载
  18. 28天高效突击大礼包:微服务+分布式+框架,java开发spark视频
  19. java中的PO、BO、VO、QO、POJO、DTO、DAO分别代表什么意思
  20. Altium Designer--如何将底层视图进行翻转

热门文章

  1. 实战药物分子筛选之一_初探
  2. php的udp数据传输,python实现udp数据报传输的方法
  3. 在线作图|如何绘制一张好看相关性矩阵图
  4. Cell子刊:粘上你-细菌生长素介导的植物根部细菌定殖
  5. ISME:多组学揭示低氧环境下的汞甲基化细菌
  6. 专属于教育界的定律,你知道哪一些?
  7. Python使用matplotlib可视化面积图(Area Chart)、通过给坐标轴和曲线之间的区域着色可视化面积图、在面积图的指定区域添加箭头和数值标签
  8. python使用pandas计算dataframe中每个分组的分位数极差、分组数据的分位数极差(range)、使用groupby函数和agg函数计算分组的两个分位数
  9. R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行独立成分分析ICA(Independent components analysis)、设置method参数为ica
  10. pandas使用isin函数和all函数判断dataframe特定数列中是否包含指定列表中的全部内容