202. Happy Number 平方循环数
[抄题]:
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 平方循环数的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode OJ 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【easy】202. Happy Number
happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- 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' ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- Nodejs调试技术总结
调试技术与开发技术构成了软件开发的基石.目前Nodejs作为新型的Web Server开发栈倍受开发者关注.总的来说Nodejs的应用程序主要有两部分:JavaScript编写的js模块和C语言编译的 ...
- RTP协议全解(H264码流和PS流)
写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...
- LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...
- Unity3D for Android 纹理压缩支持
http://blog.csdn.net/asd237241291/article/details/48548557 首先附图:Unity3D for Android支持的纹理压缩格式 纹理压缩可以通 ...
- Python函数-map()
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回.如下: def ...
- 3.Monkey Script小案例
1.实现打开搜狗搜索APP,在搜索框输入内容,点击回车,重复2次运行 2.实现代码如下所示: type=user count=10 speed=1.0 start data >> Laun ...
- VMware tools的使用
现在的开发环境基本上都是lnmp,所以我们需要在windows下使用虚拟机来搭建开发环境, 但是 在本地写好代码以后,需要上传到虚拟机的运行目录,每次修改都需要上传??? 不存在的!!! 使用Vmwa ...
- YUV转换成RGB算法
YUV转换成RGB void yuvtorgb ( double *rgb,unsigned char *yuv) { int i; rgb[] = ] + + ] - ); // r rgb[] = ...
- Linux内核 - 定时器
#include <linux/timer.h> //头文件 struct timer_list mytimer; //定义变量 static void my_timer(unsigned ...
- 几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)
以下为集中排序的java代码实现(部分是在引用别人代码): 插入排序(InsertSort): //代码原理 public static void iSort(int[] a){ for(int i ...