Happy Number——LeetCode
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
LeetCode新题,题目大意:给定一个数,将其每一位平方和相加得到一个新的数,以此循环往复,最后得到的数如果是1,则给定的数是happy number,否则会无尽的循环。
解题思路:一开始直接模拟,循环100次还不是1直接剪掉return false,也能AC,但是毕竟可能有情况覆盖不到,改用HastSet存放中间数就行了,如果得到的新结果在中间数集合中出现了,那么一定会陷入循环并且得到的不是1。
Talk is cheap>>
public boolean isHappy(int n) {
Set<Integer> res = new HashSet<>();
int sum = n;
while (sum != 1) {
n = sum;
sum = 0;
while (n != 0) {
sum += (n % 10) * (n % 10);
n /= 10;
}
if (res.contains(sum)) {
return false;
}
res.add(sum);
}
return true;
}
Happy Number——LeetCode的更多相关文章
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- Happy Number - LeetCode
examination questions Write an algorithm to determine if a number is "happy". A happy numb ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Largest Number——LeetCode
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Letter Combinations of a Phone Number——LeetCode
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
随机推荐
- js的MVC结构设计
基于jquery的Deferred,设计出如下MVC架构. 模型model interface.js interface: function(userid){ var dtd = $.Deferred ...
- android 高德地图API 之 java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLibrary returned null错误
错误场景: 运行android app时,在运行到调用高德地图API时,出现 “java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLi ...
- SQL server 如何附加、还原、分离、备份数据库文件
No1 : 附加 No2 : 还原 一.(需要 .bak文件)首先建立一个数据库,数据库名称与你的.bak文件名要相同. 当然.这时候的这个数据库还是空的,需要还原回去数据.右键 ...
- if 和 swith的选择.
具体数值不多,而是符合byte short int char这四种类型,建议使用swtich语句.因为效率稍高. 其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广.
- MVC ViewData和ViewBag
视图数据可以通过ViewBag属性访问,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类.对于ViewBag是如此的强大,意味着你能动态的s ...
- [LeetCode OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- APP被Rejected 的各种原因翻译(转)
原文:http://www.cnblogs.com/sell/archive/2013/02/16/2913341.html Terms and conditions(法律与条款) 1.1 As a ...
- 关于javascript输出中文乱码的问题
今天找到一个引导效果.原来是用英文进行引导.但是我改了里面的英文为汉字就出现乱码的情况.英文提示是在js页面里面完成的.所以最后的解决办法 就是把js文件用记事本打开,然后把文件另存为utf-8的格式 ...
- .NET Framework(一)
.NET Framework:即Microsoft .NET Framework,它是用于Windows的新托管代码编程模型.它强大功能与新技术结合起来,用于构建具有视觉上引人注目的用户体验的应用程序 ...
- 关于本地计算机无法启动Apache2
最近因工作需要,要学习PHP的基础编程,于是学习架设PHP工作环境. 但按照教材上介绍的那样,安装了WMAP后,一直无法运行成功.后发现Apache一直都不在运行状态.到WMAP中的Apache选项中 ...