LeetCode OJ -Happy Number
题目链接: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,或者原数在记录的集合中已存在,即出现重复。
- 现写的代码结构中,将原数n插入到集合中,这步应在while循环刚进入时插入,不能在循环体最后插入。
LeetCode OJ -Happy Number的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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++) ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
随机推荐
- UVa 10400 记忆化搜索
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
- linux mono
linux下.net环境; rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm &am ...
- Telephone directory - SGU 127(水)
题目大意:有一个电话簿,每页最多纪录K行电话,现在有N个电话要记录在电话薄上,要求同页的电话号码的首位要相同,电话簿的前两页是纪录的别的东西,问最少需要多少页电话簿. 分析:直接求首位数字有多少个即可 ...
- mac svn命令
转载:Mac下svn command命令 svn help command 获取子命令说明 svn info $URL 查看工作空间信息 svn list 显示当前目录下svn记录文件列表,不访 ...
- 关于用jQuery知识来实现优酷首页轮播图!
▓▓▓▓▓▓ 大致介绍 看到了一个轮播图的思路,就想的自己动手实践一下,总体来说用jQuery实现起来简单多了 如果对代码中使用的方法有疑问,可以参考我的jQuery学习之路(持续更新),里面有讲解: ...
- 如何制作iso文件
UltraISO 9.6.2.3059中文完美破解安装版 http://www.upantool.com/qidong/2011/UltraISO_v9.5.0.2800.html 软碟通v9.6.2 ...
- PPT扁平化风格设计手册
钱文嘉:颜色选择,搭配 http://www.pptfans.cn/341917.html
- CMake高速入门
入门基础:http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ 在 linux 下使用 CMake 构建应用程序 入门进阶:http ...
- Linux内核中SPI/I2c子系统剖析
Linux内核中,SPI和I2C两个子系统的软件架构是一致的,且Linux内核的驱动模型都以bus,driver,device三种抽象对象为基本元素构建起来.下文的分析将主要用这三种抽象对象的创建过程 ...
- javascript 的基本优化
http://www.ruanyifeng.com/blog/2010/01/12_javascript_syntax_structures_you_should_not_use.html