[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

对n进行 / 10等处理操作,循环条件是 n > 0

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

while (n > 0)的条件下,需要反复操作

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

hashset重复性:加不进

[关键模板化代码]:

while (n > 0) {
remain = n % 10;
squareSum += remain * remain;
n = n / 10;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean isHappy(int n) {
//cc
if (n == 0) {
return false;
} //ini
int squareSum, remain;
Set set = new HashSet(); //while loop, contains
while (set.add(n)) {
squareSum = 0; while (n > 0) {
remain = n % 10;
squareSum += remain * remain;
n = n / 10;
} if (squareSum == 1) return true;
n = squareSum;
} return false;
}
}

202. Happy Number 平方循环数的更多相关文章

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

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

  2. LeetCode OJ 202. Happy Number

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

  3. 40. leetcode 202. Happy Number

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

  4. 【easy】202. Happy Number

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

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

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

  6. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  7. leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

    一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...

  8. Java for LeetCode 202 Happy Number

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

  9. (easy)LeetCode 202.Happy Number

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

随机推荐

  1. github 第一次使用及出现的问题解决

    1.前言: 我们使用git,自然是希望我们的项目可以方便的从本地上传到git的仓库中,从而实现项目版本控制和备份,但是,从GitHub的网站上传文件,只能上传25MB的数据,我想多数人的项目都不可能只 ...

  2. mysql数据库乱码问题

    设置如下:SET character_set_client=utf8; SET character_set_results=utf8; SET character_set_connection=utf ...

  3. winform常用方法

    1.对象的初始化器: Class a = new Class() { id = , name = "张三" } 2.窗体间传值    ①构造函数    ②单例函数 //单例模式:确 ...

  4. POJ--1094--Sorting It All Out||NYOJ--349--Sorting It All Out(拓扑排序)

    NYOJ的数据水一点,POJ过了是真的过了 /* 拓扑排序模板题: 每次输入都要判断有环与有序的情况,如果存在环路或者已经有序可以输出则跳过下面的输入 判断有序,通过是否在一个以上的入度为0的点,存在 ...

  5. 生成Texture2D纹理图片

    using UnityEngine;using System.Collections; public class ProceduralTexture : MonoBehaviour{ public i ...

  6. 在C#中实现截获shell程序的输出

    在Windows环境下的所谓shell程序就是dos命令行程序,比如VC的CL.exe命令行编译器,JDK的javac编译器,启动java程序用的java.exe都是标准的shell程序.截获一个sh ...

  7. Installing Redis more properly

    Installing Redis more properly Running Redis from the command line is fine just to hack a bit with i ...

  8. ICanPay介绍

    ICanPay介绍 ICanPay是一个支持多商户多种支付方式的跨平台网关处理类库,使用ICanPay可以简化订单的创建.查询.退款和接收网关返回的支付通知等操作. 目前支持的支付网关有:支付宝(Al ...

  9. hihoCoder#1122(二分图最大匹配之匈牙利算法)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上一回我们已经将所有有问题的相亲情况表剔除了,那么接下来要做的就是安排相亲了.因为过年时间并不是很长,所以姑姑希望能够尽可 ...

  10. hihoCoder#1139(二分+bfs)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后终于准备出海捞船和敌军交战了 ...