LeetCode 解题报告--202Happy Number
1 题目描述
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
链接地址:https://leetcode.com/problems/happy-number/
2 解决方案
java代码如下:
public class Solution {
public boolean isHappy(int n) {
int result = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
boolean isLucky = false;
if (n <= 0) {
return isLucky;
}
while (true) {
int sum = 0;
int num = n;
while (num > 0) {
int temp = num%10;
num = (num -temp)/ 10;
sum = sum + temp * temp ;
} result = sum; if (result ==1 ) {
isLucky = true;
break;
}
else if(list.contains(result)) {
isLucky = false;
break;
}else {
n = result;
list.add(result);
} }
return isLucky;
}
}
LeetCode 解题报告--202Happy Number的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- leetcode解题报告(17):Missing Number
描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
随机推荐
- Debian 7 升级内核
Debian 7(wheezy)的内核是3.2,要想把内核升级到3.16怎么办呢?使用backports源! 一.添加backports源 打开/etc/apt/source.list文件,加入以下: ...
- javaweb学习总结十八(软件密码学、配置tomcat的https连接器以及tomcat管理平台)
一:软件密码学 1:对称加密 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key).对称加密有很多种算法,由于它效 ...
- javaweb学习总结十七(web应用组织结构、web.xml作用以及配置虚拟主机搭建网站)
一:web应用组织结构 1:web应用组成结构 2:安装web组成机构手动创建一个web应用程序目录 a:在webapps下创建目录web b:在web目录下创建html.jsp.css.js.WEB ...
- Android Fragment 实例
Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...
- iOS下编译ffmpeg
网络上搜索“ios ffmpeg 编译”,文章一大把,但我编译还是费了很大的功夫才编译成功.很多文章只是把步骤列了出来,但是每个人的系统环境,或者程序版本都不一样,结果出现各种的错误.我把自己编译过程 ...
- 【Knockout】四、绑定上下文
Binding context binding context是一个保存数据的对象,你可以在你的绑定中引用它.当应用绑定的时候,knockout自动创建和管理binding context的继承关系. ...
- C#算法基础之选择排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java I/O第二篇 之 (缓冲流 随机流 数组流 数据流)
1:缓冲流 BufferedReader BufferedWriter 具有较强的读写能力,能单独读写一行数据,能够减少对硬盘的访问次数. /** * 缓冲流 能够增强对问价数据的可读性,减少访问读 ...
- 优化Android应用内存的若干方法
原帖地址:http://www.open-open.com/lib/view/open1392013992317.html 在app开发的各个阶段中要考虑RAM的限制问题, 包括在设计阶段(正式开发之 ...
- 开源而又强大的迷你型web服务器推荐
appweb显然是不二之选,看了下最新版,已经到了4了 下载下来,http://appwebserver.org/software/appweb-4.4.4-0-src.tgz,十几M,直接吓傻,离我 ...