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
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
ne=0
while n>6:
while n//10>0:
ne+=(n%10)**2
n=n//10
ne+=n**2
n=ne
ne=0
return n==1

  

[LeetCode&Python] Problem 202. Happy Number的更多相关文章

  1. [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...

  2. [LeetCode&Python] Problem 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  3. [LeetCode&Python] Problem 693. Binary Number with Alternating Bits

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  4. [LeetCode&Python] Problem 136. Single Number

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  5. [LeetCode&Python] Problem 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  6. [LeetCode&Python] Problem 171. Excel Sheet Column Number

    Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...

  7. [LeetCode&Python] Problem 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  8. [LeetCode&Python] Problem 806. Number of Lines To Write String

    We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...

  9. [LeetCode&Python] Problem 682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

随机推荐

  1. 谷歌机翻英文字幕输出(Subtitle Edit)

    Subtitle Edit 下载地址(https://github.com/SubtitleEdit/subtitleedit/releases/tag/3.5.0) 添加字幕文件后,点下图的Auto ...

  2. 学习了一天的python,终于可以爬爬了-_-

    恒久恒久以前在语言大陆就听过一种叫,人生苦短,我用python的至理名言.陆陆续续在课下和业余生活中学习的一点python,知道基本的语法和规则,不过py的库实在是太多了,而且许多概念也没有深入的学习 ...

  3. flask --- 04 . 偏函数, 线程安全,栈堆,

    一.偏函数(partial) ①第一种 结果为: ② 第二种 二.线程安全 将空间转换成时间 问题: 1秒钟打印所有结果 threading.current_thread().ident :线程ID ...

  4. Mac配置Hadoop最详细过程

    Mac配置Hadoop最详细过程 原文链接: http://www.cnblogs.com/blog5277/p/8565575.html 原文作者: 博客园-曲高终和寡 https://www.cn ...

  5. 《Visual C# 从入门到精通》第二章方法和作用域——读书笔记

    第2章 方法和作用域 2.1创建方法 方法是一个基本的,强大的编程机制.可视为函数或者子程序相似的东西. 方法名是个有意义的标识符. 方法主体包含方法被调用时实际执行的语句. 声明一个方法的实例如下: ...

  6. Java自带RPC实现,RMI框架入门

    Java自带RPC实现,RMI框架入门 首先RMI(Remote Method Invocation)是Java特有的一种RPC实现,它能够使部署在不同主机上的Java对象进行通信与方法调用,它是一种 ...

  7. 【转】 SQL - 生成指定范围内的随机数

    DECLARE @Result INT DECLARE @Upper INT DECLARE @Lower INT SET @Lower = 1 SET @Upper = 10 SELECT @Res ...

  8. Python3解析dex文件

    一.说明 1.1 背景说明 看<加密与解密>的时候反复听说“PE文件格式”,到Android安全兴起就不断听说“dex文件格式”.意思是看得懂的,但自己不能手解析一番总觉得不踏实,所以决定 ...

  9. Python—字典的操作

    字典的操作: #字典的本质其实是dict类的对象 >>> a = dict([(")]) >>> a {'} 一.增加 >>> stud ...

  10. arguments.callee弃用与webuploader

    使用最近使用ueditor的时候 谷歌浏览器下上传相同图片两次后第三次上传不了 而且取消了后会出现一个错误的图片.使用的ueditor是1.4.3 后来发现 这个是 webuploader插件的问题. ...