LeetCode之“数学”: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
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
这道题比较需要注意的一点是要在合适的时候跳出死循环。程序如下:
class Solution {
public:
bool isHappy(int n) {
vector<int> vec;
vec.push_back(n);
while(n!= )
{
int sum = ;
int tmpN = n;
while(tmpN != )
{
sum += (tmpN % ) * (tmpN % );
tmpN /= ;
}
n = sum;
if(find(vec.begin(), vec.end(), n) != vec.end())
break;
else
vec.push_back(n);
}
return n == ;
}
};
改为用哈希表来实现的程序如下:
class Solution {
public:
bool isHappy(int n) {
unordered_map<int, int> hashMap;
hashMap[n] = n;
while(n != )
{
int sum = ;
int tmpN = n;
while(tmpN != )
{
sum += (tmpN % ) * (tmpN % );
tmpN /= ;
}
n = sum;
if(hashMap.find(n) != hashMap.end())
break;
else
hashMap[n] = n;
}
return n == ;
}
};
LeetCode之“数学”:Happy Number的更多相关文章
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 乘风破浪:LeetCode真题_009_Palindrome Number
乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...
- leetcode 321 Create Max Number
leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- Servlet - Upload、Download、Async、动态注册
Servlet 标签 : Java与Web Upload-上传 随着3.0版本的发布,文件上传终于成为Servlet规范的一项内置特性,不再依赖于像Commons FileUpload之类组件,因此在 ...
- [Vim]新建python文件自动添加python header
使用vim脚本来实现的,使用了模板,几行代码就能实现很实用. ~/.vimrc 中的代码 "auto add pyhton header --start autocmd BufNewFile ...
- 分布式缓存GemFire架构介绍
1什么是GemFire GemFire是一个位于应用集群和后端数据源之间的高性能.分布式的操作数据(operational data)管理基础架构.它提供了低延迟.高吞吐量的数据共享和事件分发.Gem ...
- shell的数值计算,小数计算
shell脚本中,可以进行数值计算, 如加减乘除,通过expr.let.(())等完成,文章介绍:http://blog.csdn.net/longshenlmj/article/details/14 ...
- 插件占坑,四大组件动态注册前奏(一) 系统Activity的启动流程
转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52190050 前言:为什么要了解系统Activity,Service,,BroadCa ...
- Mybatis执行BaseExecutor(二)
BaseExecutor是Executor的一个子类,是一个抽象类,其实现了接口Executor的部分方法,并提供了三个抽象方法doUpdate.doFlushStatements和doQuery在他 ...
- 字符串的n位左旋
要求:将主串的某一段(n位)翻转到主串的最后,如:abcdef以2位翻转则结果为:cdefab.要求时间复杂度为O(n),空间复杂度为O(1) 思路一:可以重新定义一个与原串相同大小的字符数组resu ...
- UNIX环境高级编程——Linux系统调用列表
以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的Linux系统调用列表,即使是简单的字母序英文列表,能做到这么完 ...
- Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...
- cocos2D v3.4 在TileMap中开启高清显示
在Tiled中制作一幅地图,用的图片砖块分辨率为32x32. iOS设备为iPhone 4s. 在未打开高清屏支持的cocos2d v2.x版本中,运行log显示480x320.遂启用高清屏支持: [ ...