【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/happy-number/
Total Accepted: 36352 Total Submissions: 109782 Difficulty: Easy
题目描述
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:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
题目大意
判断一个数字是不是开心的数字,所谓开心数字,就是把它的每一位数字求平方和之后构成新数字,然后继续这个操作,看最后能不能到1.
解题方法
递归
使用递归的方法。
我自己的算法,10以下的Happy Number 只有 1和7 ,如果一个数计算到只有个位数时,如果计算到十位以下,这个数是1或7,返回true,否则,返回false。
public static boolean isHappy(int n) {
int ans = 0;
if (n == 1 || n == 7) {
return true;
} else if (n > 1 && n < 10) {
return false;
} else {
String numString = "" + n;
char numChar[] = numString.toCharArray();
for (char aNumChar : numChar) {
ans += (aNumChar - '0') * (aNumChar - '0');
}
}
return isHappy2(ans);
}
方法一改进:
没必要10以下的数字啊,1到7之间的都是false。直接判断数到1和7之间 就false就好了。
7通过计算也回到1。
public static boolean isHappy(int n) {
int ans = 0;
if (n == 1) {
return true;
} else if (n > 1 && n < 7) {
return false;
} else {
String numString = "" + n;
char numChar[] = numString.toCharArray();
for (char aNumChar : numChar) {
ans += (aNumChar - '0') * (aNumChar - '0');
}
}
return isHappy5(ans);
}
迭代
同计算循环小数一样, 如果出现循环, 则无需继续计算,直接返回false即可.
每次计算时,把已经计算数放到一个集合里面,在计算过程中如果出现循环(集合里已经有这个数字),返回false。否则一直计算。
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
visited = set()
while n not in visited:
visited.add(n)
nx = 0
while n != 0:
nx += (n % 10) ** 2
n //= 10
if nx == 1:
return True
n = nx
return False
迭代的C++代码如下:
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> visited;
visited.insert(n);
while (n != 1) {
int pre = n;
int next = 0;
while (pre) {
next += (pre % 10) * (pre % 10);
pre /= 10;
}
n = next;
if (visited.count(n))
break;
visited.insert(n);
}
return n == 1;
}
};
日期
2015/10/16 16:06:37
2018 年 11 月 19 日 —— 周一又开始了
2019 年 1 月 14 日 —— 凛冬将至
【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)的更多相关文章
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- 2021-2-3-利用anaconda+prefetch+aspera从NCBI的SRA数据库中下载原始测序数据
目录 1.Conda连接不上镜像源问题 2. aspera不能再独立使用 3.使用prefetch搭配aspera 4. prefetch下载方法 记录下下载过程,为自己和后人避坑. 1.Conda连 ...
- 【数据处理】python将GO注释结果整理为WEGO文件
通常,比对NR库后为m8格式,通过NR和GO数据库对应关系文件,写代码整理为Gene-->GO文件,如下: 这里是一对一的关系,要转换为WEGO格式文件,即一对多关系,如下: 用python脚本 ...
- Java 好用的东西
Java自带的一些好用的东西: 求一个数的每一位:(toCharArray) int i = 10;char[] s = String.valueOf(i).toCharArray(); 十进制转二进 ...
- mysql—mysql错误Every derived table must have its own alias解决
Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名. 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的 ...
- c语言转义字符如下
#define MQTT_EVENT_REPORT_BOX_STATUS_FORMAT "{"\ ...
- 【题解】洛谷P1001 A+B Problem
第一篇博客,献给2020年的残夏. 静听8月的热情与安宁,在竞赛中的时光如白驹过隙. 也不惧未知的风雨,努力向着既往的通途. 题目地址 https://www.luogu.com.cn/problem ...
- 游戏案例|Service Mesh 在欢乐游戏的应用演变和实践
作者 陈智伟,腾讯 12 级后台专家工程师,现负责欢乐游戏工作室公共后台技术研发以及团队管理工作.在微服务分布式架构以及游戏后台运维研发有丰富的经验. 前言 欢乐游戏工作室后台是分布式微服务架构,目前 ...
- Linux下强制踢掉登陆用户
1.pkill -kill -t tty 例:pkill -kill -t tty1
- Function overloading and const keyword
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- java对象分配
1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...