【leetcode】Happy Number(easy)
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
思路:
这题目挺有意思的,肯定不能真的通过无限循环来判断。我观察了一下,估计不happy的数字,运算一圈后会出现前面已经出现过的数字,即在一组数字里绕圈圈。故记录一下出现过的数字,判断是否重复。
如果重复了就不happy,得到1了就happy。
bool isHappy(int n) {
unordered_set<int> record;
while()
{
int sum = ;
while(n > )
{
sum += (n % ) * (n % );
n /= ;
}
if(sum == )
return true;
if(record.find(sum) == record.end()) //当前数字没有出现过
{
record.insert(sum);
n = sum;
}
else
return false;
}
}
还有用O(1)空间的,就像链表找有没有圈一样,用快慢指针的思路。
public class Solution {
public boolean isHappy(int n) {
int x = n;
int y = n;
while(x>1){
x = cal(x) ;
if(x==1) return true ;
y = cal(cal(y));
if(y==1) return true ;
if(x==y) return false;
}
return true ;
}
public int cal(int n){
int x = n;
int s = 0;
while(x>0){
s = s+(x%10)*(x%10);
x = x/10;
}
return s ;
}
}
还有用数学的,所有不happy的数字,都会得到4.(??why)
bool isHappy(int n) {
if (n <= ) return false;
int magic = ;
while () {
if (n == ) return true;
if (n == magic) return false;
int t = ;
while (n) {
t += (n % ) * (n % );
n /= ;
}
n = t;
}
}
【leetcode】Happy Number(easy)的更多相关文章
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【LeetCode】Counting Bits(338)
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Course Schedule(middle)☆
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- Update-Package : Unable to load the service index for source https://api.nuget.org/v3/index.json.
由于更改了项目"属性"的"目标框架"(原来的框架是".NET Frameword4.5"改为了".NET Frameword4&q ...
- 由pthread_create引起的段错误
一般线程的结束是由进程内的其他线程来结束的,调用pthread_cancel. 但是需要考虑到被结束线程的性质,一方面,线程是可被结束,也可无法结束,即不响应该信号:另一方面,如果线程是可被结束的,那 ...
- Sturts2的action不执行任何方法的原因
今天用<s:url action="xxx">调用action的时候出现了一个“异常”, action里的任何方法都没有执行,直接返回success,而且没有任何报错. ...
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 【C语言入门教程】2.8 C 语言的预处理命令
预处理命令是在程序编译阶段进行执行的命令,用于编译与特定环境相关的可执行文件.预处理命令扩展了 C 语言,本节将选择其中一些常用的预处理命令进行讲解. 2.8.1 宏替换命令 宏替换命令的作用类似于对 ...
- Support for Xpm library: no问题
编译gd,出现问题 Support for Xpm library: no 怎么解决? 解决办法就是去下载libXpm库安装: yum -y install libXpm-dev 这 ...
- AChartEngine 安卓折线图 柱形图等利器
http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多de ...
- CSS3中动画属性transform、transition和animation
Transform:变形 在网页设计中,CSS被习惯性的理解为擅长表现静态样式,动态的元素必须借助于javascript才可以实现,而CSS3的出现改变了这一思维方式.CSS3除了增加革命性的创新功能 ...
- OOP感想
OOP是面向对象编程(Object Oriented Programming).集于一身,最终目的是各司其职,让每个职责的只关注自己那块,其他的不管丢给下一个人.比如说,一个页面,对于客户,只要能看到 ...
- qt-5.6.0 移植之qt文件系统的建立
经过差不多两个星期的奋斗,终于在板子里面跑起来了qt 程序,虽然现在还没有把触摸屏驱动加上去,但是我相信已经不远了!!!!! 在前两篇的随笔里面 , 已经编译好了最纯净的文件系统以及交叉编译完成了qt ...