[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 ...
随机推荐
- Android LayoutInflater详解 (转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- LISP学习-开发环境以及hello world
我想说说关于common lisp的开发环境问题,学习一个新的语言,如何最简单的搭建一个开发环境是至关重要的,它应该不让你在其他方面花费太多的精力,而只专注于学习语言本身. 其实我刚开始尝试的并不是c ...
- 对于一个负数mod正数
鸟神说.. a/b靠零取整 然后呢..a%b定义成a-(a/b)*b c语言就是这么算的... 那么python2.6是怎么算的呢 如果最后你取模想得到一个正数.. 那么在上述取模定义不变的情况下 p ...
- WinForm使用WebBrowser操作HTML页面的Element
前言 在 Window Form 应用程序如果需要浏览网页时可以崁入 WebBrowser 控件,但如果需要操作崁入的 HTML 的网页元素,就需要额外的操作,以下纪录几种操作 HTML 元素的方法以 ...
- CentOS安装PHP和mysql
新生在不会编译的情况下: 1.安装PHP5 yum install php 根据提示输入Y直到安装完成 2.安装PHP组件,使 PHP5 支持 MySQL yum install php-mysql ...
- HTTP基础08--追加协议
消除 HTTP 瓶颈的 SPDY HTTP 的瓶颈 Web 网站为了保存这些新增内容,在很短的时间内就会发生大量的内容更新;为了尽可能实时地显示这些更新的内容,服务器上一有内容更新,就需要直接把那些内 ...
- Nico Nico Ni~(完全背包)
Time Limit:2000MS Memory Limit:65535K Type: Program Language: Not Limited Description Lys plays L ...
- BestCoder Round #68 (div.2)
并查集 1002 tree 题意:中文题面 分析:(官方题解)把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=siz ...
- Date 对象转换——toString、toTimeString、toDateString、toUTCString、toLocaleString()、toLocaleTimeString()、toLocaleDateString()
JavaScript toString() 方法 JavaScript Date 对象参考手册 定义和用法:toString() 方法可把 Date 对象转换为字符串,并返回结果. 语法:dateOb ...
- eclipse 和 android studio 打包签名apk问题
首先,我使用eclipse打包了一个签名apk 然后,我使用同一个签名文件在android studio 打包同一个项目 接下来,首先安装eclipse 打包的apk,然后安装android stud ...