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)的更多相关文章

  1. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  2. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 【leetcode】Min Stack(easy)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  6. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  7. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. Sphinx扩展安装安装

    Coreseek官方教程中建议php使用直接include一个php文件进行操作,事实上php有独立的sphinx模块可以直接操作coreseek(coreseek就是sphinx!)已经进入了php ...

  2. 郝斌C语言代码

    #include<stdio.h> int main() { ; printf("%#x\n",a); ; } /* output 0xf; */ //(15)10= ...

  3. C#中页面之间传值传参的六种方法

    QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能用这 ...

  4. Eclipse常见配置及常用插件

    tomcat为能同时运行多个项目而不崩溃,需要配置一下jvm设置 -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m

  5. cocos布局分析

    HBox和VBox布局 HBox只是一个水平布局包装类. HBox里面所有的孩子节点都会水平排列成一行 VBox仅仅是对垂直布局的一个简便的类封装. VBox把它的子节点布局在一竖列中. Layout ...

  6. loadrunner 学习笔记--AJAX(转)

    用loadrunner测试WEB程序的时候总是会碰到AJAX或者ActiveX实现的功能,而通常这些功能会包含很多客户端函数(一般为JavaScript).我们该如何处理?如果从功能实现的角度去考虑这 ...

  7. Java网络编程学习

    服务器是指提供信息的计算机或程序,客户机是指请求信息的计算机或程序,而网络用于连接服务器与客户机,实现两者相互通信.但有时在某个网络中很难将服务器与客户机区分开.我们通常所说的“局域网”(Local ...

  8. EF接触03

    emdx文件解读:

  9. 在 Ubuntu 16.04 上安装 LEMP 环境之图文向导

    导读 LEMP 是个缩写,代表一组软件包(注解 ① L:Linux OS,E:Nginx 网络服务器,M:MySQL/MariaDB 数据库和 P:PHP 服务端动态编程语言),它被用来搭建动态的网络 ...

  10. 剑指Offer 反转链表

    题目描述 输入一个链表,反转链表后,输出链表的所有元素.     思路: 法1:用栈,压栈出栈 法2:头插法(有递归非递归2中)   AC代码: /* struct ListNode { int va ...