lintcode:快乐数
快乐数
写一个算法来判断一个数是不是"快乐数"。
一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是无限循环但始终变不到1。如果可以变为1,那么这个数就是快乐数。
19 就是一个快乐数。
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
解题
定义最大循环次数方法
public class Solution {
/**
* @param n an integer
* @return true if this is a happy number or false
*/
public boolean isHappy(int n) {
// Write your code here
if(n<=0)
return false;
if(n==1)
return true;
ArrayList<Integer> list = new ArrayList<Integer>();
int nextNum = n;
int limit = 100;
while(limit>0){
nextNum = nextNum(nextNum);
if(nextNum == 1)
return true;
limit--;
}
return false;
}
public int nextNum(int n){
int nextNum = 0;
while(n!=0){
nextNum +=(n%10)*(n%10);
n/=10;
}
return nextNum;
}
}
用TreeSet保存链表上的数,当出现循环而不到1 一定不是快乐数
public class Solution {
/**
* @param n an integer
* @return true if this is a happy number or false
*/
public boolean isHappy(int n) {
// Write your code here
if(n<=0)
return false;
if(n==1)
return true;
TreeSet<Integer> set = new TreeSet<Integer>();
int nextNum = n;
while(set.add(nextNum)){
nextNum = nextNum(nextNum);
if(nextNum == 1)
return true;
}
set.clear();
return false;
}
public int nextNum(int n){
int nextNum = 0;
while(n!=0){
nextNum +=(n%10)*(n%10);
n/=10;
}
return nextNum;
}
}
这样增加了空间复杂度
Project Euler 92:Square digit chains
有这样的一句话:任何一个到达1或89的数字链都会陷入无尽的循环。更令人惊奇的是,从任意数开始,最终都会到达1或89。
所以直接判断是否能够到达1 还是89,到达1就是快乐数,达到89就不是的了
public class Solution {
/**
* @param n an integer
* @return true if this is a happy number or false
*/
public boolean isHappy(int n) {
// Write your code here
if(n<=0)
return false;
if(n==1)
return true;
int nextNum = n;
while(true){
nextNum = nextNum(nextNum);
if(nextNum == 1)
return true;
if(nextNum == 89)
return false;
} }
public int nextNum(int n){
int nextNum = 0;
while(n!=0){
nextNum +=(n%10)*(n%10);
n/=10;
}
return nextNum;
}
}
lintcode:快乐数的更多相关文章
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- lintcode-【简单题】快乐数
题目: 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...
- leetcode python快乐数
编写一个算法来判断一个数是不是“快乐数” “快乐数”的定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复该过程直到为1,也可能是无限循环但始终变不到1. 如果可以变为1,那 ...
- [Swift]LeetCode202. 快乐数 | Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Leetcode 202.快乐数 By Python
编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 ...
- 力扣(LeetCode)202. 快乐数
编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 ...
- 【leetcode 简单】 第五十六题 快乐数
编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如 ...
- Leecode刷题之旅-C语言/python-202快乐数
/* * @lc app=leetcode.cn id=202 lang=c * * [202] 快乐数 * * https://leetcode-cn.com/problems/happy-numb ...
随机推荐
- 基于swift语言iOS8的蓝牙连接(初步)
看过一些蓝牙App的事例,大体上对蓝牙的连接过程进行了了解.但是开始真正自己写一个小的BLE程序的时候就举步维艰了.那些模棱两可的概念在头脑中瞬间就蒸发了,所以还是决定从最基本的蓝牙连接过程进行.这里 ...
- maven学习手记 - 1
学习目标 windows下安装maven环境: 使用命令创建maven项目结构: maven项目编译测试打包安装运行: 在maven项目中使用插件. 在windows下安装maven环境 在win ...
- 安装v2meet客户端 进入会议依然 提示 您还未安装视频会议的客户端,请下载安装
解决办法 1.安装软件,要用管理员权限安装 2.装一个360浏览器,登录会议,这样就成功了.原装IE9却不行. 估计是IE9做了一些安全限制,由于时间关系就没有再处理了.
- 20145120 《Java程序设计》第8周学习总结
20145120 <Java程序设计>第8周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点 read()将ReadableByteChannel中的数据读至By ...
- canvas画时钟
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 【Merge Two Sorted Lists】cpp
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- cas 登陆超时 解决方案
在配置文件ticketExpirationPolicies.xml中配置: <bean id="grantingTicketExpirationPolicy" class=& ...
- 【转载】错误:ORA-28002: the password will expire within 7 days 解决方法
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:xwdreamer 原文地址: 错误:ORA-28002: the ...
- Java Applet使用
问题描述: Java Applet使用 参考资料: http://docs.oracle.com/javase/tutorial/deployment/applet/depl ...
- ngcordova 监控网络制式改变
ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...