1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第二百零二题

(1)题目
英文:
Write an algorithm to determine if a number n 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.

Return True if n is a happy number, and False if not.

中文:
编写一个算法来判断一个数 n 是不是快乐数。

「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。

如果 n 是快乐数就返回 True ;不是,则返回 False 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/happy-number

(2)解法
如果tmp在sets中已经存在了,则直接返回False,因为会无限循环的,内存会爆
(耗时:48ms,内存:13.6M)

class Solution:def isHappy(self, n: int) -> bool:sets=[]tmp=nwhile True:tmp=self.s(tmp)if tmp==1:return Trueif tmp in sets:return Falsesets.append(tmp)def s(self,n):res=0while n/10>0:res+=(n%10)*(n%10)n=n//10return res

注意:
1.s函数是用于求n的各位数的平方和的

leetcode python3 简单题202. Happy Number相关推荐

  1. leetcode python3 简单题136. Single Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百三十六题 (1)题目 英文: Given a non-empty array ...

  2. leetcode python3 简单题9. Palindrome Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第九题 (1)题目 英文: Determine whether an integer ...

  3. leetcode python3 简单题191. Number of 1 Bits

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...

  4. leetcode python3 简单题171. Excel Sheet Column Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十一题 (1)题目 英文: Given a column title as ...

  5. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

  6. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

  7. leetcode python3 简单题204. Count Primes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...

  8. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  9. leetcode python3 简单题167. Two Sum II - Input array is sorted

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

最新文章

  1. 推荐系统中的隐语义模型
  2. 查看SQL Server Resource Database以及修改系统表
  3. Qt实现应用程序单实例运行--LocalServer方式
  4. ssh 用密码连接不上、查看openSSH 的版本、CentOS - 升级openSSH,修复安全漏洞
  5. myrocks复制中断问题排查
  6. 为什么在生产中进行调试是如此诱人?
  7. java自动依照日期建表,脚本根据一个表中的日期字段填充每月汇总表
  8. 如何实现消息功能_如何实现微信小程序的轮盘抽奖功能
  9. 每个选项设置上限的投票_使用Redis有序集合实现投票排行榜系统
  10. python安装-Python安装
  11. 【Eclipse】Eclipse-Build-缓慢-卡住
  12. 华硕FX60VM笔记本系统安装
  13. 我的世界pc要安装java_我的世界安装教程 PC版新手安装攻略
  14. TCP/IP、Http的区别--(转自任智康)
  15. 360面临两线作战----手机安全领域硝烟再起
  16. Go初体验-实现平方根函数
  17. openGL中向量的加减乘除运算
  18. windows无法格式化u盘_U 盘格式化提示 windows 无法完成格式化的解决办法
  19. 编程之美1:那些关于1的个数的经典面试题
  20. 最小生成树算法超详细教程

热门文章

  1. php连接mysql数据库输出_PHP连接并输出数据库数据实例代码
  2. oracle+gsm安装,针对Oracle的10G版本提升SCOTT为DBA脚本!
  3. Tensorflow:TensorFlow基础(一)
  4. 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛——E题 Seek the Joker II
  5. 实验8.2 队列类的定义与实现
  6. 实例分割——转置卷积的学习笔记
  7. GitLab CI简单示例
  8. Tensorflow中的name_scope和variable_scope
  9. 【Qt教程】2.2 - Qt5 布局管理器(水平、垂直、栅格布局)、弹簧、设计一个登陆界面
  10. MySQL学习记录 (四) ----- SQL数据管理语句(DML)