题目链接:https://leetcode.com/problems/happy-number/

题目理解:实现isHappy函数,判断一个正整数是否为happy数

  happy数:计算要判断的数的每一位的平方和,平方和为1则为happy数,不为1则将原数替换为此平方和,继续上述步骤,直到等于1(为happy数),或者进入不包含1的无限循环(非happy数)。

  测试用例:19为happy数:

解题思路:

定义set集合存每次计算得到的需判断数n,循环计算新的原数n,终止条件为n等于1或者set中出现重复的数字。循环体:将n插入到set中,循环取n的每一位计算其平方和,并将n等于这个平方和。最后循环结束,如果n等于1,则原数为happy数,否则不是。

代码:

 class Solution {
public:
bool isHappy(int n) {
set<int> result;
int re = ;
while(n!=&&!result.count(n)){
result.insert(n);
re = ;
while(n){
int a = n%;
re+=a*a;
n = n/;
}
n = re;
}
if(n==) return true;
else return false;
}
};

卡住的点:

  1. 判断是否出现不为1的死循环,需要记录每次的原数,循环终止条件为原数为1,或者原数在记录的集合中已存在,即出现重复。
  2. 现写的代码结构中,将原数n插入到集合中,这步应在while循环刚进入时插入,不能在循环体最后插入。

LeetCode OJ -Happy Number的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { int i,j; ; i<n; i++) { ; j<n; j++) ...

  6. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  7. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  8. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  9. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

随机推荐

  1. git 添加忽略文件

    使用github for windows客户端添加.gitignore文件:   如下图所示,在github客户端可以看到未提交的更改列表 随便选中一个文件,右链,选择ignore file. 然后会 ...

  2. Maven学习(2) - Maven构建多模块Java工程

    概述 项目开发时,通常会将项目分为多个模块进行开发,本文讨论如何用Maven构建多模块的Java工程. 软件环境 Java:1.6.0_26 Maven:3.1.1 OS:WindowXP SP3 项 ...

  3. 对于利用pca 和 cca 进行fmri激活区识别的理解

    1.pca 抛开fmri研究这个范畴,我们有一个超长向量,这个超长向量在fmri研究中,就是体素数据.向量中的每个数值,都代表在相应坐标轴下的坐标值.这些坐标轴所组成的坐标系,其实是标准单位坐标系.向 ...

  4. 【转】Java中如何遍历Map对象的4种方法

    原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...

  5. 用Delphi7开发Web Service程序 转

        转:http://rosehacker.blog.51cto.com/2528968/450160 用Delphi7开发Web Service程序,并把服务程序放在IIS Web服务器上提供给 ...

  6. Linq中小心使用IndexOf

      我们平常在做字符串的模糊查询时,有可能会用到下面的类似LINQ写法: string.IsNullOrEmpty(_SN) ? true : a.SN.IndexOf(_SN) != -1   这条 ...

  7. Bzoj 2748: [HAOI2012]音量调节 动态规划

    2748: [HAOI2012]音量调节 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1234  Solved: 777[Submit][Status ...

  8. Ubuntu 下安装opencv 编译后执行找不到库

    在ubuntu下编译opencv程序后,执行报下面到错误:error while loading shared libraries: libopencv_core.so.2.4: cannot ope ...

  9. 使用Calendar获取近三年的财务信息

    1.

  10. java的任务监听器监听任务

    Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行.一般用的较少 监听 ...