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 ...
随机推荐
- Android 高级UI设计笔记09:Android如何实现无限滚动列表
ListView和GridView已经成为原生的Android应用实现中两个最流行的设计模式.目前,这些模式被大量的开发者使用,主要是因为他们是简单而直接的实现,同时他们提供了一个良好,整洁的用户体验 ...
- Java(Android)编程思想笔记01:多态性的理解
1. 多态的定义: 指允许不同类的对象对同一消息做出响应.即同一消息可以根据发送对象的不同而采用多种不同的行为方式. (发送消息就是函数调用) 2. 多态的理解 多态是面向对象的重要特性,简 ...
- WdatePicker时间控件联动选择
$("#txtStartTime").bind("click focus", function () { var endtimeTf = $dp.$('txtE ...
- iOS xcode6添加预编译文件
在xcode6以后,由于苹果不建议开发者乱用预编译文件,所以,在项目创建之后 就不会自动生成预编译文件. 那么如果我们想要使用预编译文件,就需要自己动手来添加.那到底该如何为我们的项目添加预编译文件呢 ...
- 关于Java序列化和Hadoop的序列化
import java.io.DataInput; import java.io.DataOutput; import java.io.DataOutputStream; import java.io ...
- Wince 中的图形编程
图形编程程序当中,笔者主要要和大家讨论的是画刷的创建和使用以及绘图函数,比如2D图像的绘制等等. *画刷的定义: HBRUSH hBrush; *画刷的类型: 1. 系统内置画刷:GetStockOb ...
- javascript+dom 做javascript图片库
废话不多说 直接贴代码 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- HDOJ2014青年歌手大奖赛_评委会打分
青年歌手大奖赛_评委会打分 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- <转载>批处理之FOR语句祥解
批处理之FOR语句祥解 FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号) FOR 参数 %%变量名 IN ...
- asp.net字符串分割函数用法
先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我要根据[111cn.net]分割的 ...