[LeetCode] 202. Happy Number 快乐数
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: 19 is a happy number
- 1^2 + 9^2 = 82
- 8^2 + 2^2 = 68
- 6^2 + 8^2 = 100
- 1^2 + 0^2 + 0^2 = 1
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
写一个算法判断一个数是不是快乐数。快乐数:一个正整数,如果对其各个位上的数字分别平方求和得到一个新的数,再进行同样的操作,如果结果变成了1,则说明是快乐数。
解法:循环求平方和,即求出当前数组的平方和后,再以此平方和作为新的数继续求平方和,而循环终止条件是:得到的平方和为1,或得到的平方和在之前的循环中出现过,那以后会一直循环,不可能达到1。判断平方和是否为1很简单,每次检查就好了;而判断平方和是否出现过,则只需要维持一个Set,每次循环检查当前平方和是否在Set中,在则终止循环,不在则将此平方和放到Set中。
Java:
public class Solution {
public boolean isHappy(int n) {
Set<Integer> got = new HashSet<>();
while (n != 1 && !got.contains(n)) {
got.add(n);
int sum = 0;
while (n != 0) {
sum += Math.pow(n % 10, 2);
n /= 10;
}
n = sum;
}
return n == 1;
}
}
Python:
class Solution:
# @param {integer} n
# @return {boolean}
def isHappy(self, n):
lookup = {}
while n != 1 and n not in lookup:
lookup[n] = True
n = self.nextNumber(n)
return n == 1 def nextNumber(self, n):
new = 0
for char in str(n):
new += int(char)**2
return new
Python:
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
got = set()
while n != 1 and n not in got:
got.add(n)
sum = 0
while n:
sum += (n % 10)**2
n //= 10
n = sum return n == 1
C++:
class Solution {
public:
bool isHappy(int n) {
set<int> got;
while (n != 1 && got.find(n) == got.end()) {
got.insert(n);
int sum = 0;
while (n) {
sum += pow(n % 10, 2);
n /= 10;
}
n = sum;
}
return n == 1;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 202. Happy Number 快乐数的更多相关文章
- 202 Happy Number 快乐数
写一个算法来判断一个数是不是“快乐数”.一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,或是无限循环但始终变不到 1.如 ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 136. Single Number 单独数
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...
随机推荐
- Docker 部署 vue 项目
Docker 部署 vue 项目 Docker 作为轻量级虚拟化技术,拥有持续集成.版本控制.可移植性.隔离性和安全性等优势.本文使用Docker来部署一个vue的前端应用,并尽可能详尽的介绍了实现思 ...
- DT6.0框架留言模块漏洞修复
今天早上登入后台,留言被国外乱码注入一大堆,很烦人,得去数据库清空.所以仔细检查dt的留言模块,找到解决办法. 在:module/extend/guestbook.inc.php 大约第10行左右 i ...
- CLR如何将SEH异常映射到托管异常类型
托管异常处理构建在Windows操作系统的结构化异常处理之上,通常称为SEH.这意味着CLR了解如何在SEH和托管异常系统之间进行互操作,这是一个非常关键的点,因为SEH基于异常代码的概念,而托管异常 ...
- C# await async Task
//原文:https://www.cnblogs.com/yan7/p/8401681.html //原文:https://www.cnblogs.com/s5689412/p/10073507.ht ...
- 洛谷P1577 切绳子题解
洛谷P1577 切绳子题解 题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位(直接舍掉2为后的小数). 输入输出格 ...
- CF1237E 【Balanced Binary Search Trees】
首先我们要注意到一个性质:由于根与右子树的根奇偶性相同,那么根的奇偶性与\(N\)相同 然后我们发现对于一个完美树,他的左右两个儿子都是完美树 也就是说,一颗完美树是由两棵完美树拼成的 注意到另一个性 ...
- JavaScript高级程序编程(二)
JavaScript 基本概念 1.区分大小写,变量名test与Test 是两个不同的变量,且函数命名不能使用关键字/保留字, 变量命名规范: 开头字符必须是字母,下划线,或者美元符号,ECMAScr ...
- mysql 唯一键
唯一键特点: 1.唯一键在一张表中可以有多个. 2.唯一键允许字段数据为NULL,NULL可以有多个(NULL不参与比较) //一个表中允许存在多个唯一键,唯一键允许为空,在不为空的情况下,不允许重复 ...
- OpenFOAM——90度T型管
本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL010: Laminar Flow in a 90° Tee-Junction. ...
- hosts 屏蔽定位域名
通过修改hosts屏蔽定位服务的域名 #屏蔽百度地图 1.0.0.1 api.map.baidu.com 1.0.0.1 ps.map.baidu.com 1.0.0.1 sv.map.baidu.c ...