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. CLGeocoder "Lost connection to geod" #error# when use geocodeAddressString:completionHandler

      I got this warning when I tried to get destination using CLGeoCoder and the warning is coming out ...

  2. Google Protocol Buffer 简单介绍

    以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...

  3. OS X Framework Library not loaded: 'Image not found'的解决办法

    参考:OS X Framework Library not loaded: 'Image not found' 1.首先将相应的framework手动复制到/System/Library/Framew ...

  4. weapp微信小程序初探demo

    https://github.com/donglegend/weapp-demo 参考文档开发工具安装微信weapp API git项目源码微信小程序 demo效果展示效果预览

  5. iOS开发初级课程

    iOS开发初级课程 针对学员 掌握Objective  C,C或者C++,有语言基础的学员,想从事iOS开发工作. iOS开发那些事-了解iOS开发(8集) 在课程中,我们首先介绍如何使用nib和故事 ...

  6. Eclipse高级使用技巧

    1. Eclipse的配置文件导入和导出功能 说明:可以将Eclipse的自定义的工作空间配置文件导出和导入,这样创建多工作空间时候就省去了再设置的烦恼. 2. Eclipse设置显示行号 说明:设置 ...

  7. BZOJ3196——二逼平衡树

    1.题目大意:给你一个序列,有5种操作,都有什么呢... 1> 区间第k小 这个直接用二分+树套树做 2> 区间小于k的有多少 这个直接用树套树做 3> 单点修改 这个直接用树套树做 ...

  8. ORA-02287: 此处不允许序号

    今天使用 insert into select 时出现了这个异常,感觉很诡异,去metalink查了下资料,找出了错误原因,记录下来. SQL> CREATE TABLE test_baser0 ...

  9. JNI_Android项目中调用.so动态库实现详解

    转自:http://www.yxkfw.com/?p=7223 1. 在Eclipse中创建项目:TestJNI 2. 新创建一个class:TestJNI.java package com.wwj. ...

  10. C#构造方法重载

    1.什么是构造方法? 首先,它是一个方法,它是类中 众多方法中的一个.其次,它具有类中其他方法所不具备的一些特性. 在类执行开始的时候,执行这个方法. 2.构造方法相对其他方法有哪些不同? 方法名:类 ...