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 ...
随机推荐
- OpenCV程序在Debug时出现「PDB文件无法加载」的一个解决方法
这几天毕设要用到OpenCV,按照网上的教程来搭建开发环境. 用的是OpenCV 3.0 beta + Visual Studio Community 2013.我的系统64位是Win 8.1,但在加 ...
- My网页
开始更新|Version:2.46|更新内容:/=====================================//1.新增秒低价次数//2.优化捉鬼停留过久的问题//3.优化其他任务上的效 ...
- 关于reportng生成的测试报告不按测试执行顺序的解决办法
需要修改TestResultComparator类的源码,源码自己去reportng官网http://reportng.uncommons.org/下载,因为reportng默认是按字母先后顺序进行排 ...
- FOUNDATION_EXPORT 延伸
一般iOS我们定义常量的方法有两种,来看下面例子 我的.h文件 1 2 FOUNDATION_EXPORT NSString * const kMyConstantString; FOUNDATI ...
- 《JavaScript高级程序设计》读书笔记 ---RegExp 类型
ECMAScript 通过RegExp 类型来支持正则表达式.使用下面类似Perl 的语法,就可以创建一个正则表达式.var expression = / pattern / flags ; 其中的模 ...
- 对adapter的封装优化
一般不优化的adapter通常继承自BaseAdapter会出现一下几个问题: getCount(), getItem(), getItemId()代码都要去重写,一个adapter还行,如果adap ...
- Commix命令注入漏洞利用
介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏 ...
- JS复习:第八章
一.全局作用域: 所有在全局作用域中声明的变量.函数都会变成window对象的属性和方法.全局变量不能通过delete操作符删除,而window对象上定义的属性可以. 二.窗口大小 使用resizeT ...
- Apache Zeppelin
介绍 用于做数据分析和可视化 一.二进制安装 1)下载二进制包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/incubator/zeppelin/0 ...
- hdu 1408 盐水的故事
Problem Description 挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下:然后滴二滴,停一下:再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴 ...