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

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

Solution: 辅助函数;循环计算每位数字的平方和,直到出现结果为1(返回true)或者重复(返回false)

 class Solution {
public:
bool isHappy(int n) {
map<int,bool> m;
int ret=sum(n);
while(ret!=){
if(m[ret]==true)return false;
m[ret]=true;
ret=sum(ret);
}
return true;
}
int sum(int n){
int ret=;
while(n){
ret += (n%)*(n%); //不支持(n%10)^2
n /= ;
}
return ret;
}
};

【LeetCode】202 - Happy Number的更多相关文章

  1. 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

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

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

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

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

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

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

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

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

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【一天一道LeetCode】#202. Happy Number

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

  8. 【刷题-LeetCode】202. Happy Number

    Happy Number Write an algorithm to determine if a number n is "happy". A happy number is a ...

  9. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

随机推荐

  1. postgreSQLG关闭活动的connection、删除活动的数据库

    First, find the activities that are taken place against the target database, you can query thepg_sta ...

  2. ExtJS4之Ext.MessageBox的各种用法

    1. Ext.Msg.alert(String title,String msg)[Ext.MsssageBox.alert(Sting title,Sting msg,fn)]与javascript ...

  3. C# Server.MapPath()

     ./当前目录 /网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot   应用程序虚拟目录为E:\wwwroot\company 浏览的页面路径为E:\wwwroot\ ...

  4. c语言类型转换注意事项

    转载自: http://blog.csdn.net/zhuimengzh/article/details/6728492 1.隐式转换     C在以下四种情况下会进行隐式转换:        1.算 ...

  5. centos chrome

    在centos6.X和redhat enterprise 中安装chrome,我找了很久都不行,今天终于找到了可以用下脚本那安装: #! /bin/bash # Google Chrome Insta ...

  6. Android 的 ramdisk.img、system.img、userdata.img 作用说明,以及UBoot 系统启动过程

    首先通過編譯,先將android內核編譯成功.正常情況下,在目錄out/target.product/generic/(但是有的就沒有generic文件,如freescale和iriver:但是lon ...

  7. How to: Read Object Data from an XML File

    This example reads object data that was previously written to an XML file using the XmlSerializer cl ...

  8. 2-Medium下的MultipleCommandAssembly

    1.首先创建一个控制台项目 2.添加引用[红色的都是将项目添加为引用,其中蓝色的log4net是直接从源代码下的Reference文件夹下引用的dll] 3.写代码 1)首先将config配置好[ht ...

  9. explicit constructor(显示构造函数)

    按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应的数据类型的数据转换为该类对象,如下所示: class String { String(const char* p) //用C ...

  10. C#基本数据类型

    在第一章我们了解了C#的输入.输出语句后,我这一节主要是介绍C#的基础知识,本节的内容也是后续章节的基础,好的开端等于成功的一半.在你阅读完本章后,你就有足够的C#知识编写简单的程序了.但还不能使用继 ...