题目链接: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. 中国版 Azure 现提供 Azure Traffic Manager

    Stephen MaloneAzure网络 - DNS和 Traffic Manager高级项目经理 我们非常高兴地宣布,中国版 Azure中现已提供 Azure Traffic Manager.Az ...

  2. Linux Shell编程(27)——子shell

    运行一个shell脚本时会启动另一个命令解释器. 就好像你的命令是在命令行提示下被解释的一样, 类似于批处理文件里的一系列命令.每个shell脚本有效地运行在父shell(parent shell)的 ...

  3. SolrJ总结

    1.solrJ概念 solrJ是Java连接solr进行查询检索和索引更新维护的jar包. 2.项目引入solrJ相关jar包 对于maven工程,直接将下面内容加入到pom文件中即可. <de ...

  4. 网络流CodeForces. Original 589F:Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  5. ACM2096

    #include<iostream> int main() { using namespace std; int a,b,count; cin>>count; while(co ...

  6. C#三种判断数据库中取出的字段值是否为空(NULL) 的方法

    操作数据库,需要判断返回的字段值是否为空,收集了3种方法供参考 1 通过System.DBNull判断,网上大部分都使用这个方法. DataTable dt;                     ...

  7. DevExpress LookUpEdit 下拉框基本操作

    <span style="font-size:14px;"> ArrayList list = new ArrayList(); //遍历皮肤,放到列表中 foreac ...

  8. 关于java里小数点的保留

    关于java里小数点的保留 1.先给大家看一个代码. import java.util.*;import java.text.*; public class A { public static voi ...

  9. win7重装系统时,使用PE工具箱进入系统看到的“C盘变成0.2G,D盘变成48G左右”这是什么回事?

    引入: 今天帮同学重装系统,重装系统使用的方法是利用PE工具箱制作出启动U盘,进行重装系统. 我的步骤是 第一步:开机按F2挂载U盘优先启动,于是开机时就进入PE微系统 第二步: 用分区工具(Disk ...

  10. C# SQL多条件查询拼接技巧

    本文转载:http://blog.csdn.net/limlimlim/article/details/8638080 #region 多条件搜索时,使用List集合来拼接条件(拼接Sql) Stri ...