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. go 编译:交叉编译&编译执行过程

    1. 交叉编译 编译Windows程序和mac程序 GOOS=windows GOARCH-amd64 go build main.go 转自:https://www.cnblogs.com/mafe ...

  2. onclick 事件

    onclick 事件 Event 对象 定义和用法 onclick 事件会在对象被点击时发生. 请注意, onclick 与 onmousedown 不同.单击事件是在同一元素上发生了鼠标按下事件之后 ...

  3. centos7安装node

    centos7安装node 二进制文件安装 node=v10.13.0 file=node-${node}-linux-x64 wget https://nodejs.org/dist/${node} ...

  4. HDFS初次编程

    hadoop是用Java语言实现的开源软件框架,可以支持多种语言,我学习的时候用得自然就是Java了. 在开始编程之前需要做一些配置工作: Hadoop开发:Hadoop为HDFS和Mapreduce ...

  5. 异步async/await简单应用与探究

    感谢Marco CAO指出的两点错误,已做出修改与补充 异步函数(async/await)简单应用 .NET Framework4.5提供了针对异步函数语法糖,简化了编写异步函数的复杂度. 下面通过一 ...

  6. python 字符串与16进制 转化

    def str_to_hex(s): return r"/x"+r'/x'.join([hex(ord(c)).replace('0x', '') for c in s]) def ...

  7. C/C++.判断文件是否存在(_access)

    1. int _access(char* path,int mode)头文件<io.h>功能:确定文件或文件夹的访问权限.如果指定的存取方式有效,则函数返回0,否则函数返回-1. 参数pa ...

  8. java jdk动态代理学习记录

    转载自: https://www.jianshu.com/p/3616c70cb37b JDK自带的动态代理主要是指,实现了InvocationHandler接口的类,会继承一个invoke方法,通过 ...

  9. liunx存储管理之基础知识

    存储基础知识 ====================================================================================主要知识点: 基本 ...

  10. 完整的Django入门指南学习笔记6

    前言 欢迎来到系列教程的第六部分!在这篇教程中,我们将详细探讨基于类的视图(简称CBV).我们也将重构一些现有的视图,以便利用内置的基于类的通用视图(Generic Class-Based Views ...