leetCode(29):Happy Number
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
class Solution {
public:
bool isHappy(int n) {
map<int,int> value;
while(n!=1)
{
value[n]=1;
int remain;
int tmp=0;
while(n!=0)
{
remain=n%10;
tmp+=remain*remain;
n=n/10;
}
n=tmp;
if(value.find(n)!=value.end())
break;//是否已经出现过
}
if(n!=1)
return false;
return true;
}
};
leetCode(29):Happy Number的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- [LeetCode] 136. Single Number 单独数
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
随机推荐
- POJ1716 贪心
题目大意:在[0,10000]上给出n个区间,要求在区间选整数点,每个区间至少包含两个点,问至少要几个点.题目保证有解决方案. 题目分析: 我们做过在区间上至少包含一个点的题目.类似的方法,我们先排序 ...
- 在活动之间切换(隐式Intent)
实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...
- [bzoj1017][JSOI2008]魔兽地图 DotR (Tree DP)【有待优化】
Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...
- 去除两端margin的方法
假如有一份视觉稿,其中一部分是实现这样的: 两排横向排列的框,它们中间有边距,两端无边距.每个框大小为100*100,外边距为20,整个区域为460*220. HTML结构: <div> ...
- zabbix install
Auth: Jin Date: 20140714 用了5 6年的监控工具 http://zabbix.org/wiki/InstallOnCentOS_RHEL Server Install yum ...
- SecureCRT也能和Xshell一样批量导入主机
在Xshell可以像这样一个文件批量导入主机: https://blog.netsarang.com/91/importing-csv-formatted-host-information-to-xs ...
- JavaScript Diagramming
JavaScript Diagramming Optensity
- Spring Bean init-method 和 destroy-method实例
在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时.这是用来替代 InitializingBean和Di ...
- Index downloads are disabled, search results may be incomplete.
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...
- java解析json数组
java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...