LeetCode OJ: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 + 81 = 82
- 64 + 4 = 68
- 36 + 64 = 100
- 1 + 0 + 0 = 1
欢乐数,就是不断将位的平方相加为新数,看看最后得到的数是不是1,是的话就是欢乐数,否则不是。代码比较简单,用一个set保存下来已经求到过的不是1的数字,一旦
后期某个计算到的数字能在set中找到(不是1),那么就一定进入到死循环当中了。这是就应该返回false, 否则一旦某个计算结果是1那么久返回true,代码见下:
class Solution {
public:
bool isHappy(int n) {
set<int> sample;
sample.insert(n);
int s = ;
for(;;){
while(n != ){
s += (n % ) * (n % );
n /= ;
}
if(s == )
return true;
else{
if(sample.find(s) != sample.end())
return false;
sample.insert(s);
n = s;
s = ;
}
}
}
};
java版本如下所示,方法相同:
public class Solution {
public boolean isHappy(int n) {
HashSet<Integer> set = new HashSet<Integer>();
do{
if(set.contains(n))
return false;
set.add(n);
int tmp = 0;
while(n != 0){
tmp += (n%10)*(n%10);
n/=10;
}
n = tmp;
}while(n!=1);
return true;
}
}
LeetCode OJ:Happy Number(欢乐数)的更多相关文章
- [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 OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- [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 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...
- LeetCode OJ 之 Number of Digit One (数字1的个数)
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode OJ:Number of 1 Bits(比特1的位数)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- (转)java类到底是如何加载并初始化的?
Java虚拟机如何把编译好的.class文件加载到虚拟机里面?加载之后如何初始化类?静态类变量和实例类变量的初始化过程是否相同,分别是如何初始化的呢?这篇文章就 是解决上面3个问题的. 若有不正之处, ...
- Android--Apache HttpClient(2种实现)
前言 上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持,现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织 ...
- image_Magic图片处理功能
:] 来自为知笔记(Wiz)
- (转)CTO的烦恼:为啥差距就这么大呢?
话说胖哒是一只CTO,近来遇到了一些小烦恼… 胖哒表示这么多问题想想就头大啊! 一直听说用Docker解决问题不错,于是两个月前,胖哒不远万里来到DockerCon 16,打算向国外的Docker先行 ...
- Oracle 在64位机器上使用plSQL连接Oracle的问题(SQL*Net not properly installed)
问题: 在64位机器上了64位的oracle客户端. 然后装上PL/SQL Developer,但是连接oracle老报这个错: Initialization error SQL*Net n ...
- PAT 天梯赛 L1-015. 跟奥巴马一起画方块 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-015 AC代码 #include <iostream> #include <cstdio&g ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 杭电1027Ignatius and the Princess II模拟
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1027 题目: Problem Description Now our hero finds the doo ...
- grads 新老版本目录对比
最近不少人都在使用OpenGrADS,最新的版本已经更新到了2.0.a9,具体grads做了哪些更新,在附件里面放了一个,是英文的. 很多人说在使用原来的一些教程的时候找不到相对应的文件夹了,其实仔细 ...
- Linux常用命令(6/26)——dd命令和split命令
dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 以可选块长度复制文件,默认情况下从标准输入设备输出到标准输出设备.复制过程中,还可以对文件进行一些转换. dd命令可以指定block的 ...