题目链接: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. 使用VisualStudio进行单元测试之四 顺序测试

    前文中所提到的测试都是针对一个方法进行的独立测试,即使是同事测试多个方法,他们之间也没有影响.但是在实际的生产过程中,更多的情况是方法与方法之间是存在相互的逻辑关系的,所以也就有了今天要介绍的顺序测试 ...

  2. 使用doxygen生成注释文档

    1. doxygen下载地址:http://www.stack.nl/~dimitri/doxygen/ 2. 参考http://wenku.baidu.com/link?url=ETvBUyaR9f ...

  3. HDOJ/HDU 2562 奇偶位互换(交换位置~)

    Problem Description 给定一个长度为偶数位的0,1字符串,请编程实现串的奇偶位互换. Input 输入包含多组测试数据: 输入的第一行是一个整数C,表示有C测试数据: 接下来是C组测 ...

  4. 【Javascript&Jquery基础归纳】- 加载相关

    1.window.onload 必须等到Dom所有元素.包括图片加载完毕后加载,只能编写一个. 2.$(document).ready()      DOM结构加载完毕后马上执行,并且可以编写多个. ...

  5. 【原】hive 操作笔记

    1.建表: hive> CREATE TABLE pokes (foo INT, bar STRING); hive> CREATE TABLE invites (foo INT, bar ...

  6. 【转】VS2013编译libjpeg库

    原文地址:http://blog.csdn.net/weixinhum/article/details/42718959 现在,很多图像处理工具和开源库都给出了图像解码的函数接口,然而有时这些接口并不 ...

  7. hdoj 2094 产生冠军

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. winform 播放声音方式 分类: WinForm 2014-07-25 14:16 194人阅读 评论(0) 收藏

    声音文件folder.wav放置在bin目录下debug下 1.通过API调用 [c-sharp] view plaincopy using System.Runtime.InteropService ...

  9. java中的上转型对象

    1. 定义 如果B类是A类的子类或间接子类,当用B类创建对象b并将这个对象b的引用赋给A类对象a时,如: A a;a = new B();ORA a;B b = new B();a = b; 通俗的说 ...

  10. iOS 百度地图 小的特点demo

    先上图的样子 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDEyMzIwOA==/font/5a6L5L2T/fontsize/400/fill ...