Problem:

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

Summary:

若一个整数的各个位数的平方和相加,不断重复这个操作后为1,则这个数为happy number。给出一个数n,判断其是否为happy number。

Analysis:

一个数重复各个位数平方和相加的操作为1时为happy number,那么判断一个数不是happy number的依据,则为各个位数平方和相加无论多少次,依旧不能为1。那么一定是在相加的过程中出现了数的循环,1不含在循环中。

所以只需重复平方和操作,判断每个数的各个位数平方和计算完毕后所得结果有无出现过,若从未出现,则继续计算,否则判断此数不是happy number。

 class Solution {
public:
bool isHappy(int n) {
unordered_map<int, int> m;
while (true) {
int sum = ;
while (n) {
sum += (n % ) * (n % );
n /= ;
} n = sum;
if (m[sum]++ > ) {
break;
}
} return n == ;
}
};

LeetCode 202 Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  7. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

随机推荐

  1. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  2. Android中设定背景图片平铺。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在做Android开发时,我们常常需要为程序设定一个背景,但由于现在的Android设备尺寸不一,如果随便设置一个图片为背景,那么很 ...

  3. 清北暑假模拟day2 国

    [题目描述]在世界的东边,有三瓶雪碧.--laekov黎大爷为了虐 zhx,给 zhx 出了这样一道题.黎大爷搞了一个数据结构,但是他没有告诉 zhx 这到底是什么数据结构,我们只知道这是一个数据结构 ...

  4. 机器码call和jmp地址的计算

    call和jmp都是跳转指令,但是call的同时会把pc地址压入堆栈,并且这两种方式都有远和近跳转.下面的分析不全,因为没有在网上找到足够的资料,个人创造这个情景还是有些困难. 1.例子中的call的 ...

  5. List对象排序通用方法

    import java.util.Collections; import java.util.Comparator; import java.util.List; import java.lang.r ...

  6. getRow()方法

    getRow :不是返回行数,而是返回当前是哪一行

  7. 特殊字符导致用正则表达式进行字符串替换失败,Java replaceAll()方法报错Illegal group reference

    String str = "给商品||?>\\n阳澄湖大闸蟹!@#$%^&*()_+-=?:\",.]\\|~.,\/??\\\\|\\br点赞" Stri ...

  8. Swift3.0P1 语法指南——集合类型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  9. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  10. js中的json

    1.什么是JSON? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 2.JSON语法是JavaScr ...