1、题目

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

Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

2、分析

这个题目每次会把每位的平方相加,然后循环这样,然后知道这个数字等于1为止,经过我们举例分析,如果不是快乐数最终或者中途出现这些数字,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中

3、代码实现

public class Solution {public boolean isHappy(int n) {if (n <= 0)return false;int result = 0;while (n != 0) {int temp = n % 10;result += temp * temp;n = n / 10;}if (result == 1) return true;else if (result == 4 || result == 16  || result == 37 || result == 58 || result == 89 || result == 145 || result == 42 || result == 20 )return false;else return isHappy(result);}
}

LeetCode之Happy Number相关推荐

  1. LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...

  2. 【整数转字符串】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...

  3. 【To Do! 重点 正则表达式】LeetCode 65. Valid Number

    LeetCode 65. Valid Number 本博客转载自:[1]http://www.cnblogs.com/yuzhangcmu/p/4060348.html [2]https://blog ...

  4. 【回文串2】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1:我的答案 思路一:转化为字符串 class Solution { public:bool isPalindrome(in ...

  5. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

  6. 【异或】LeetCode 137. Single Number II

    LeetCode 137. Single Number II Solution1:不会做,抄的 博客转载自:http://www.cnblogs.com/grandyang/p/4263927.htm ...

  7. 【异或】LeetCode 136. Single Number

    LeetCode 136. Single Number Solution1:我的答案 还好异或的性质没记错,还好,还好 class Solution { public:int singleNumber ...

  8. [LeetCode]179. Largest Number

    [LeetCode]179. Largest Number 题目描述 解题思路 求最大的数,在数组中对于每一位数字数值越大应当越靠前,如:9 > 5,所以9应该在5之前 需要考虑的是对于不同位数 ...

  9. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

  10. LeetCode: 871. Minimum Number of Refueling Stops

    LeetCode: 871. Minimum Number of Refueling Stops 题目描述 A car travels from a starting position to a de ...

最新文章

  1. 高通的专利霸权要到头了?
  2. Django model查询之F,Q操作
  3. Webkit Flex伸缩盒模型属性备忘
  4. 逻辑备库的Swichover和Failover
  5. python字符串转date,在Python上将字符串转换为Date类型
  6. Qt工作笔记-QPlainTextEdit中数据的获取
  7. Django 2.0 学习(12):Django 模板语法
  8. RP2836 板卡信息标识
  9. busybox date 时间的加减
  10. WPS怎么统计相同名称的数据_群发邮件平台的数据统计怎么用
  11. 2017百度之星资格赛:1002. 度度熊的王国战略
  12. IBM参与马鞍山模式创新 为中国医疗信息化立新示范
  13. 6.携程架构实践 --- 数据库
  14. react-navigation之动态修改title的内容
  15. 微软Exchange Server 0Day漏洞,尽快修复
  16. 小米推送,华为推送,个推,阿里云推送集成(服务端JAVA开发)
  17. 需求分析 - 01外卖配送系统
  18. Python图像处理库PIL的基本概念介绍(一)
  19. vscode 使用 SDCC 开发 STM8
  20. 嵌入式远程岗位、兼职、接单、众包平台

热门文章

  1. 基于ABP落地领域驱动设计-03.仓储和规约最佳实践和原则
  2. Id都是“とつくとき”这样的怎么爬,在线等,急
  3. 你见过的“垃圾”项目是这样子么?
  4. Visual Studio SnippetDesigner使用
  5. 2020.NET开发者大会大会线上同步直播,以及参会秘籍
  6. 跟我一起学.NetCore之MediatR好像有点火
  7. 使用ML.NET模型生成器来完成图片性别识别
  8. 酸吗?28岁程序员财务自由宣布退休!
  9. “兼职”运维的常用命令
  10. 四种为HttpClient添加默认请求报头的解决方案