[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 ...
随机推荐
- P3375 模板 KMP字符串匹配
P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...
- swagger2 注解说明
整体说明 用于controller类上 注解 说明 @Api 协议集描述 方法上 注解 说明 @ApiOperation - @ApiImplicitParams 方法上 @ApiImplicitPa ...
- .Net Core控制台生成exe能独立运行
.Net Core控制台生成exe能独立运行,依赖文件都单独生成在一个publish文件夹里 方式一:强烈推荐,能独立运行,依赖DLL也会生成出来,支持无安装环境也能到处运行 按win+R输入cmd在 ...
- 记一次用pip安装docker-compose报错及解决方法
Docker-Compose 的安装 方法一 # 下载1.25.0 docker compose sudo curl -L "https://github.com/docker/compos ...
- 学习Microsoft Visio(2)
常用业务设计图示法 一.业务设计基础 1.名词概念 业务流程图:在公司.部门.岗位的层面上描述一个业务流程的宏观过程. 基本流程图:对某个处理过程的详细逻辑流程进行描述. 静态业务对象图(UML表示法 ...
- SQL中的trim函数
Oracle TRIM函数是很常见的函数,下面对Oracle TRIM函数的语法作了详尽的阐述说明,希望可以让您对Oracle TRIM函数有更深的认识. 如果提到Oracle TRIM函数,最简单的 ...
- jquery ajax请求数据超时设置
var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...
- window对象方法(alert-confirm-prompt)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AJax和JQ的结合使用
第一种经典模式 <%-- Created by IntelliJ IDEA. User: 60590 Date: 2019/12/4 Time: 16:08 To change this tem ...
- 使用css3变量创建炫酷悬停效果
原文地址:www.zcfy.cc/article/stunning-hover-effects-with-css-variables 效果: 主要使用css中的var做动画效果,代码如下: <! ...