LeetCode OJ 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
happy number是把一个数的各个位置的数字的平方相加,直到值为1为止。如果一个数是happy number,那么最终的结果一定会是1,如果不是,这个过程将一直持续下去,并且会重复出现以前曾经出现过的数。例如2。
22 = 4
42 = 16
12 + 62 = 37
32 + 72 = 58
52 + 82 = 89
82 + 92 = 145
12 + 42 + 52 = 42
42 + 22 = 20
22 + 02 = 4
···
所以2不是一个happy number。我们的思路就是记录这个过程中出现过的每一个数,如果某些数字在过程中重复出现,则该数肯定不是happy number。如果最后结果是1,则该数是happy number。代码如下:
public class Solution {
public boolean isHappy(int n) {
List<Integer> list = new ArrayList<>();
while (n != 1) {
if (list.contains(n)) {
return false;
}
list.add(n);
n = toArrayAndSum(n);
}
return true;
}
public static int toArrayAndSum(int n) {
int sum = 0;
while (n > 0) {
int x = n % 10;
n = n / 10;
sum = sum + x * x;
}
return sum;
}
}
LeetCode OJ 202. Happy Number的更多相关文章
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【一天一道LeetCode】#202. Happy Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】202 - Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode OJ 之 Ugly Number II (丑数-二)
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- LeetCode OJ:Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- LeetCode OJ:Ugly Number II(丑数II)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode OJ:Happy Number(欢乐数)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- github import repository创建github仓库
现在,假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆. 首先,登陆GitHub,创建一个新的仓库,名字叫blog: 1.先创建一个项目仓库 2. 我们勾选Initialize th ...
- Web多客户端单点登录
一 数据库 除了用户表之外,新建一个外联表<用户票据表> fdUsTiUserID,fdUsTiType,fdUsTiTicket 分别对应用户ID,客户端类型(PC,mobile) 票 ...
- Unity3D 导出的apk进行混淆和加固防止反编译
前言: 对于辛辛苦苦完成的apk程序被人轻易的反编译了,那就得不偿失了,这篇文章就是解决Unity打包出来的apk进行代码加固和混淆. 准备资料: 1:Obfuscator.zip 作用是代码混淆, ...
- UITextField 设置 placeholder 的字体颜色方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #ff2608 } [_textField setValu ...
- Commix命令注入漏洞利用
介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏 ...
- Maven搭建springMVC+spring+hibernate环境
这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xm ...
- Hack写法
文章来源: http://www.w3cplus.com/css/create-css-browers-hacks 条件注释:http://www.w3cplus.com/create-an-ie-o ...
- 花括号中的json数据--->转为数组array
//正式服务器 更换航班 public function changef(){ $str = '{"airportTax":50,"arriModifyTime" ...
- 关于c++中方法名前面的双冒号
#include "iostream" using namespace std; template <typename T> void swap(T &a, T ...
- 评测:VPS推荐digitalocean和Vultr和Linode
美国vps推荐三家,分别是digitalocean.vultr和linode,拥有很高的性价比,中国访问速度快.我是上面三家的早期用户,并且一直使用至今,积累了不少使用经验. DigitalOcean ...