快乐数

写一个算法来判断一个数是不是"快乐数"。

一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为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:快乐数的更多相关文章

  1. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. [LeetCode] Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. lintcode-【简单题】快乐数

    题目: 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...

  4. leetcode python快乐数

    编写一个算法来判断一个数是不是“快乐数” “快乐数”的定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复该过程直到为1,也可能是无限循环但始终变不到1. 如果可以变为1,那 ...

  5. [Swift]LeetCode202. 快乐数 | Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. Leetcode 202.快乐数 By Python

    编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 ...

  7. 力扣(LeetCode)202. 快乐数

    编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 ...

  8. 【leetcode 简单】 第五十六题 快乐数

    编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如 ...

  9. Leecode刷题之旅-C语言/python-202快乐数

    /* * @lc app=leetcode.cn id=202 lang=c * * [202] 快乐数 * * https://leetcode-cn.com/problems/happy-numb ...

随机推荐

  1. OC中的消息传递和初始化

    [receiver message]:[接收者 消息] 把消息传递给接收者. getter(接收器),setter(设置器):只设置和读取一个参数. Person *p1 = [[Person all ...

  2. JavaScript 将多个引用(样式或者脚本)放入一个文件进行引用

    1.将样式放入一个文件进行引用 @import url("../media/css/bootstrap.min.css"); @import url("../media/ ...

  3. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  4. 008--VS2013 C++ 位图半透明化(另一种显示)

    注:主要变化是在下面这张位图上的操作 //全局变量HBITMAP bg, girl;HDC mdc;//起始坐标const int xstart = 50;const int ystart = 20; ...

  5. 学习IOS需要知道的事

    什么是iOS iOS是一款由苹果公司开发的操作系统(OS是Operating System的简称),就像平时在电脑上用的Windows XP.Windows 7,都是操作系统 那什么是操作系统呢?操作 ...

  6. WIN7笔记本利用命令AP热点

    第一步:以管理员身份运行命令提示符:开始”---搜索栏输入“cmd----右键以“管理员身份运行”自己随便命名,第二步:运行命令:netsh wlan set hostednetwork mode=a ...

  7. 结对开发--课堂练习--c++

    一.题目与要求 题目: 返回一个整数数组中最大子数组的和. 要求: 入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值. ...

  8. 我给女朋友讲编程html系列(1) -- Html快速入门

    Html是一门编写网页的语言. 在我学习Html的时候,网上的很多朋友说,直接使用[记事本]编写就行了,最后保存为后缀名为 .html 的文件. 不过,我建议你用[Nodepad++]来编写网页,用这 ...

  9. openfire插件开发之完美开发

    http://redhacker.iteye.com/blog/1919329 一.说在前面 在继上篇Openfire3.8.2在eclipse中Debug方式启动最简单的方式后,我研究了openfi ...

  10. pcxFirefox 自定义

    便携特性(ini设置)     把与firefox.exe同文件夹的tmemutil-sample.ini 改名为tmemutil.ini,设置如下: Portable=1 #便携式 Portable ...