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. xshell的优化和连接

    远程连接linux ###远程连接工具---xshell/SecureCRT/Putty 商业版(收费) 家庭版(免费) 一.xshell进行优化 1.打开文件属性 2.设置终端类型问linux,输入 ...

  2. 原生JS实现简易转盘抽奖

    我爱撸码,撸码使我感到快乐. 大家好,我是Counter. 本章带大家来简单的了解下原生JS实现转盘抽奖. 因为主要涉及到JS,在这里HTML和CSS起到的功能就没有那么重要, 因此,没有过多的阐述H ...

  3. [BZOJ2298]problem a

    Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接下来n行每行两个 ...

  4. QT 设置应用程序名称和主窗口标题

    1.设置应用程序名称 在工程文件.pro文件中,修改Target为想设置的名称 TARGET = MXEditer 2.设置主窗口标题,在main文件中,我的主窗口是MainWindow. int m ...

  5. CSS 简介 3

    css css 外边距属性 margin 简写属性 在一个声明中设置所有外边距属性 margin-bottom 设置元素的下外边距 margin-left 设置元素的左外边距 margin-right ...

  6. U3D外包公司:Ruby的未来

    那是一段醉人的日子.元素数据编程,动态类型,Ruby和Rails组合而带来的约定优于配置(Convention Over Configuration)的模式,给了我们更加快速的开发和更简洁的代码.已经 ...

  7. SSM获取表单数据插入数据库并返回插入记录的ID值

    以下指示插入操作以及获取记录值的ID的部分操作代码!!! 首先是简单的表单实现 <%@ page language="java" contentType="text ...

  8. nodejs点滴

    1.exports与module.exports http://cnodejs.org/topic/5231a630101e574521e45ef8 因为require指向了module.export ...

  9. appDesign

    1原则 简约,实用,懒人模式 2模块划分 以实用目的,而非以工具

  10. WPF InkCanvas 书写毛笔效果

    https://www.cnblogs.com/younShieh/p/10602787.html