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 ...
随机推荐
- IntelliJ IDEA配置tomcat【全程详解】
相关博客:IntelliJ IDEA创建Maven+SSM+Tomcat+Git项目[全程详解] 创建好web项目后,需要将项目部署到Tomcat中运行. 接下来,图文解析IntelliJ IDEA如 ...
- 简单使用location.hash的方法 ,怎么做,有什么用? 简单的js路由页面方法。
hash 属性是一个可读可写的字符串,该字符串是URL的锚部分(从#号开始的部分).语法location.hash刚开始我真不知道hash有什么用,直到我在项目中遇上一个最大的问题.而且很恶心的体验 ...
- 201621123014《Java程序设计》第九周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 答: 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答: ...
- python中join函数用法
str.join(list/tuple/dict/string) str = "-"; seq = ("a", "b", "c&q ...
- MySQL多种安装方式选择
1.rpm包安装方式 rpm包的安装方式非常简单,这里以el6平台下的mysql-5.6.34版本为例,首先,要通过上述搜狐镜像地址下载到如下四个MySQL相关软件安装包. a.下载安装包 MySQL ...
- [Luogu3727]曼哈顿计划E
luogu 题意(简化版) 给你一棵树,每个点上有一个\(SG\)值,问你是否存在一条路径使得\(SG\)异或和为\(0\). sol 可以当做每个点的稳定值就是这个点上的石子数量. 很显然我们只需要 ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- Azure CLI脚本查看未挂载的ManagedDisk
本文介绍如何用Azure CLI的脚本查看未挂载的Managed Disk,以及Managed Disk挂载到哪些资源. 具体的脚本如下: #!/bin/bash rm -rf noownerdisk ...
- Java基础--比较器Comparator
Comparable接口和Comparator接口都是用来定义集合中的排序的,只是Comparable是在集合内部定义排序的实现,Comparator是在集合外部排序的实现. Comparable 的 ...
- 机器学习:模型泛化(岭回归:Ridge Regression)
一.基础理解 模型正则化(Regularization) # 有多种操作方差,岭回归只是其中一种方式: 功能:通过限制超参数大小,解决过拟合或者模型含有的巨大的方差误差的问题: 影响拟合曲线的两个因子 ...