[LintCode] 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^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
LeetCode上的原题,请参见我之前的博客Happy Number。
解法一:
class Solution {
public:
/**
* @param n an integer
* @return true if this is a happy number or false
*/
bool isHappy(int n) {
set<int> s;
while (n != ) {
int t = ;
while (n) {
t += (n % ) * (n % );
n /= ;
}
n = t;
if (s.count(n)) break;
else s.insert(n);
}
return n == ;
}
};
解法二:
class Solution {
public:
/**
* @param n an integer
* @return true if this is a happy number or false
*/
bool isHappy(int n) {
while (n != && n != ) {
int t = ;
while (n) {
t += (n % ) * (n % );
n /= ;
}
n = t;
}
return n == ;
}
};
[LintCode] Happy Number 快乐数的更多相关文章
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 202 Happy Number 快乐数
写一个算法来判断一个数是不是“快乐数”.一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,或是无限循环但始终变不到 1.如 ...
- [LintCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...
- lintcode:快乐数
快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...
- [Swift]LeetCode202. 快乐数 | Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202: 快乐数 Happy Number
题目: 编写一个算法来判断一个数是不是 "快乐数". 一个 "快乐数" 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直 ...
- LeetCode 快乐数(Happy Number)
题目描述 编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不 ...
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- Hibernate 和 快照
Hibernate我们已经学了四天,该讲的知识我们已经讲完,明天放假,后天练习一天就要结束hibernate的学习,有点不舍的,想来hibernate也不是传说中的那么难,在次将hibernate中三 ...
- LoadRunner之安装、破解、汉化教程(一)
安装前,把所有的杀毒软件和防火墙关闭 安装前,把所有的杀毒软件和防火墙关闭 安装前,把所有的杀毒软件和防火墙关闭........ 注意事项: 安装前,把所有的杀毒软件和防火墙关闭. 若以前安装 ...
- express-18 路由
简介 路由是网站或Web服务中最重要的一个方面:路由是将请求(由URL和HTTP方法指定)路由到处理它们的代码去的一种机制. 路由过去是基于文件的,这很简单,但不灵活. IA 是指内容的概念性组织.在 ...
- js简版图片左右切换功能
function leftMove(obj,direction,lis){ var num = 0; var liWidth = lis[0].offsetWidth; setInterval(fun ...
- PHP、Java对称加密中的AES加密方法
PHP AES加密 <?php ini_set('default_charset','utf-8'); class AES{ public $iv = null; public $key = n ...
- Robotium ant 报错Unable to find instrumentation info for: ComponentInfo{project/android.test.InstrumentationTestRunner}
[echo] Running tests ... [exec] INSTRUMENTATION_STATUS: id=ActivityManagerService [exec] INSTRUMENTA ...
- 转载:robotium typeText与enterText区别
solo.typeText和solo.enterText方法都可以对EditeText进行测试,达到的测试目的是一样的.存在几点不同: 1.实现上,typeText方法是robotium框架调用系统I ...
- Codeforces 567C Geometric Progression(思路)
题目大概说给一个整数序列,问里面有几个包含三个数字的子序列ai,aj,ak,满足ai*k*k=aj*k=ak. 感觉很多种做法的样子,我想到这么一种: 枚举中间的aj,看它左边有多少个aj/k右边有多 ...
- Eclipse: The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
Link: http://stackoverflow.com/questions/22756153/the-superclass-javax-servlet-http-httpservlet-was- ...
- word 批量修改表格格式
For i = 1 To ActiveDocument.Tables.Count ActiveDocument.Tables(i).Cell(1, 1).Select With Selection . ...