【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/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- bluetooth sig bluetooth asia-深圳之行
18年5月30日深圳参见蓝牙展会 主要了解下面 使用蓝牙和区块链构建室内导航定位系统和去中心化的MESH网络 -- 核心是通过iBeacon 来广播数据,典型用例是手机对手机的使用蓝牙进行交互,业界称 ...
- Java 读取TXT文件的多种方式
1).按行读取TXT文件package zc;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFound ...
- echarts中饼状图数据太多进行翻页
echarts饼状图数据太多 echarts 饼状图内容太多怎么处理 有些时候,我们饼状图中echarts的数据可能会很多. 这个时候展示肯定会密密麻麻的.导致显示很凌乱 我们需要'翻页'类似表格展示 ...
- linux系统中安装JDK
安装之前的准备工作 查看系统中之前安装好的JDK java –version rpm -qa | grep java 卸载JDK (以java-1.7.0-openjdk-1.7.0.45-2.4.3 ...
- Spark Stage 的划分
Spark作业调度 对RDD的操作分为transformation和action两类,真正的作业提交运行发生在action之后,调用action之后会将对原始输入数据的所有transformation ...
- 【leetocode】55. Jump Game
You are given an integer array nums. You are initially positioned at the array's first index, and ea ...
- linux 6.5 网卡
启动网卡 ifup eth0 eth0:网卡名称 设置网卡开机启动 vi /etc/sysconfig/network-scripts/ifcfg-eth0 ONBOOT=yes
- go channel 概述
精髓 将资源读进内存-->共享内存,一个个进程/线程进行处理,这是常见模式.go channel 是一种直接在进程/线程之间传递资源的方式,即以通信来共享内存.这便是go的精髓. 扩展-一些名词 ...
- Spring是如何保证同一事务获取同一个Connection的?使用Spring的事务同步机制解决:数据库刚插入的记录却查询不到的问题(转)
前言 关于Spring的事务,它是Spring Framework中极其重要的一块.前面用了大量的篇幅从应用层面.原理层面进行了比较全方位的一个讲解.但是因为它过于重要,所以本文继续做补充内容:Spr ...
- linux系统的一些常用命令
cd 进入某个目录 ifconfig 查看本机的ip cp (要复制的文件的位置) (要把文件复制的位置) ll 查看文件下,文件的操作权限 ls查看该文件夹下的有那些文件和文件夹 vi filena ...