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 ...
随机推荐
- oracle创建表空间、用户和表以及sys和system的区别
一.oracle的3个内置账号(口令管理)scott(示范账户) tiger 内置账号system 系统管理员 操作用户sys 超级管理员 操作数据 conn system/sasa;show use ...
- 两款高性能并行计算引擎Storm和Spark比較
对Spark.Storm以及Spark Streaming引擎的简明扼要.深入浅出的比較,原文发表于踏得网. Spark基于这种理念.当数据庞大时,把计算过程传递给数据要比把数据传递给计算过程要更富效 ...
- App doesn't auto-start an app when booting the device in Android
From Android 3.1, newly installed apps are always put into a "stopped" state and the only ...
- ubuntu 配置tomcat 实测成功
https://blog.csdn.net/qq_24091555/article/details/75077781
- json数据结构
JSON即JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合服务器与JavaScript的交互.JSON易于人阅读和编写.同时也易于机器解析和生成.JSON采 ...
- 支持Access的数据库建模工具 EZDML
支持Access的数据库建模工具 EZDML 下载地址:EZDML v1.5
- 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...
- SQuirrel-GUI工具安装手册-基于phoenix驱动
背景描述: SQuirrel sql client 官方地址:http://www.squirrelsql.org/index.php?page=screenshots 一个图形界面的管理工具 安装手 ...
- discuz对PHP7不支持mysql的兼容性处理
PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...
- Linux服务器配置秘钥对连接
[root@check2 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to sa ...