题目链接

  题目要求:

  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的更多相关文章

  1. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. 乘风破浪:LeetCode真题_009_Palindrome Number

    乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...

  4. leetcode 321 Create Max Number

    leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...

  5. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  6. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  8. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

随机推荐

  1. 27 自定义View小结

    自定义View 1 为了满足开发需要 就需要自定义View 2 分类: 直接继承View 继承View的子类(现有控件 button,TextView-.) 继承ViewGroup(线性布局 相对布局 ...

  2. spark运算结果写入hbase及优化

    在Spark中利用map-reduce或者spark sql分析了数据之后,我们需要将结果写入外部文件系统. 本文,以向Hbase中写数据,为例,说一下,Spark怎么向Hbase中写数据. 首先,需 ...

  3. Android开发学习之路--Drawable mutations

      时间过得很快,明天终于可以拿到房子了,交完这次房租,也可以成为房东了,看看博客也好久没有更新了,最近一直在整机器人,也没有太多时间整理博客.   今天下午和同事一起遇到了一个问题,就是明明没有改变 ...

  4. android:shape属性详解

    这一类的shape定义在xml中 file location: res/drawable/filename.xml The filename is used as the resource ID.(这 ...

  5. Android下拉列表控件spinner-andoid学习之旅(十一)

    废话不多说,下拉列表常用的就是spinner控件. 直接上代码: package peng.liu.testview; import android.app.Activity; import andr ...

  6. J2EE进阶(十)SSH框架整合常见问题汇总(一)

    SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...

  7. Hessian源码分析--总体架构

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用的是二进制协 ...

  8. Android初级教程小案例之单选框RadioGroup与复选框CheckBox

    Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version=&qu ...

  9. ledisdb:支持类redis接口的嵌入式nosql

    ledisdb现在可以支持嵌入式使用.你可以将其作为一个独立的lib(类似leveldb)直接嵌入到你自己的应用中去,而无需在启动单独的服务. ledisdb提供的API仍然类似redis接口.首先, ...

  10. 多进程log4cxx区分日志

    多进程log4cxx区分日志 (金庆的专栏) 网游客户端一般会多开,多个进程会写同一个日志文件.log4cxx看来会对文件加锁,防止多进程写同一文件写乱,截止目前还没发现错乱的日志. log4cxx有 ...