[抄题]:

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. c++primer 第四章编程练习答案

    4.13.1 #include<iostream> struct students { ]; ]; char grade; int age; }; int main() { using n ...

  2. skynet coroutine 运行笔记

    阅读云大的博客以及网上关于 skynet 的文章,总是会谈服务与消息.不怎么看得懂代码,光读这些文字真的很空洞,不明白说啥.网络的力量是伟大的,相信总能找到一些解决自己疑惑的文章.然后找到了这篇讲解 ...

  3. 关于/usr/bin/ld: cannot find -lcrypto 的错误

    Linux下 build code 时,要做 -lssl, -lcrypto 的链接,出现类似下面的错误: /usr/bin/ld: cannot find -lcrypto /usr/bin/ld: ...

  4. asp.net mvc中model注意事项

    1 modelState必须是需要在action Filter中才生效 2 发送接口的json nullable的类型必须初始化

  5. .net remoting和wcf自托管——一个bug引发的警示

    一.解决问题,需要深入,并从细节入手,多从代码找原因,不能认为代码是死的,不会出错: 之前代码都运行良好,突然某一天,在我电脑上出问题了.出了问题,那就应该找出原因.其实这个问题,本身并不难,好歹给你 ...

  6. Linux-CentOS7 安装VMware Workstation 12

    转自:http://blog.csdn.net/aoshilang2249/article/details/48656107 1.下载VMware 衔接地址 http://www.vmware.com ...

  7. C#异步编程(五)异步的同步构造

    异步的同步构造 任何使用了内核模式的线程同步构造,我都不是特别喜欢.因为所有这些基元都会阻塞一个线程的运行.创建线程的代价很大.创建了不用,这于情于理说不通. 创建了reader-writer锁的情况 ...

  8. linux开发核心理解

    目录 授权 致谢 序言 更新纪录 导读 如何写作科技文档 I. 气候 1. GUI? CLI? 2. UNIX 缩写风格 3. 版本号的迷雾 4.   Vim 还是 Emacs 5.   DocBoo ...

  9. Windbg内核调试之二: 常用命令

    运用Windbg进行内核调试, 熟练的运用命令行是必不可少的技能. 但是面对众多繁琐的命令, 实在是不可能全部的了解和掌握. 而了解Kernel正是需要这些命令的指引, 不断深入理解其基本的内容. 下 ...

  10. git公钥生成以及与coding等联合

    更好用的 https://segmentfault.com/a/1190000002645623 其中 $ ssh -T git@github.comgitbub $ ssh -T git@git.c ...