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 ...
随机推荐
- 对ASIHTTPRequest的封装
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/quanqinayng/article/details/37659751 .h文件 // // Ht ...
- CDN 环境下获取用户IP方法
CDN 环境下获取用户IP方法 1 cdn 自定义header头的X-Real-IP,在后端使用$http_x_real_ip获得 proxy_set_header X-Real-IP $remote ...
- blogCMS整理
一.在urls中写路由 二.返回登录页面(login.html中写前端代码) - username(用户名) - password(密码) - validCode(验证码) -submit(提交按钮) ...
- day3-python-文件操作(2)
本文内容涉及python中的os模块和os.path模块的常用操作,这两个模块提供了与平台和操作系统无关的文件系统访问方法.os模块负责大部分的文件系统操作,包括:删除文件.重命名文件.遍历目录树等: ...
- Apache添加多端口
Apache\conf 目录下 添加端口监听 Vhost.conf简单写写
- SharePoint 2010 以Jquery Ajax方式更新SharePoint列表数据!
之前本人的博客介绍了<sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式>,那如何通过Jquery提交访问日志到自定义的SharePoint的访问 ...
- HDU - 6321 Problem C. Dynamic Graph Matching (状压dp)
题意:给定一个N个点的零图,M次操作,添加或删除一条边,每一次操作以后,打印用1,2,...N/2条边构成的匹配数. 分析:因为N的范围很小,所以可以把点的枚举状态用二进制表示集合.用一维数组dp[S ...
- AngularJS post传值后台获取不到
AngularJS post传值后台获取不到 一般的思路: 解决办法: 1.设置一下default head 2.参数转换一下data:$.param({orderNo: orderNo,sessi ...
- Loopback接口的作用
Loopback接口是虚拟接口,是一种纯软件性质的虚拟接口.任何送到该接口的网络数据报文都会被认为是送往设备自身的.大多数平台都支持使用这种接口来模拟真正的接口.这样做的好处是虚拟接口不会像物理接口那 ...
- 【笔记】H5自适应(待)
参考: 1.盒子模型:http://www.cnblogs.com/sunyunh/archive/2012/09/01/2666841.html 2.浮动:http://www.w3school.c ...