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的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. 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 ...

  5. leetcode解题报告(15):Third Maximum Number

    描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...

  6. 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 ...

  7. 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 ...

  8. leetcode解题报告(17):Missing Number

    描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...

  9. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

随机推荐

  1. hive-通过Java API操作

    通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...

  2. 在SSIS 的 64 位版本中不支持 Excel 连接管理器

    Microsoft sql server 2008 R2——> SQL SERVER Business Intelligence Development Studio 使用EXCEL数据源或目标 ...

  3. myeclipse web 包名保留字与servlet冲突

    包名不能取modify ..........等保留字 不能有数字

  4. Objective-C ,ios,iphone开发基础:几个常用类-NSString

    第一个字符串: //必须在字符串的前面加上@符号, NSString* str=@"shouqiang_Wei";//输出以%@输出. NSLog(@"%@", ...

  5. 关于FastStone Capture输入中文出现乱码.

    关于FastStone Capture 中输入中文出现乱码. 根据我的使用,公司用的生产机是英文操作系统,这个时候用FSCapture输入中文就是乱码.英文是正常的. 自己的机器是中文的.输入中文和英 ...

  6. Java 自带性能监控工具:监视和管理控制台jconsole的使用

    关于JConsole工具的使用请参见:http://blog.csdn.net/defonds/article/details/45064297

  7. 虚拟机 VirtualBox 自制帮助文档

    初学 VirtualBox ,网络上教程很多,图片一张一张的费流量,讲得又比较散,于是花了一下午制作了此 CHM 帮助文档. 下载:(图片另存为--重命名为 RhinoC.rar --解压缩)

  8. Linux 命令 - traceroute: 数据报传输路径追踪

    traceroute 工具用于跟踪数据报的传输路径:当数据报从一台计算机传向另一台计算机时,会经过多重的网关,traceroute 命令能够找到数据报传输路径上的所有路由器.通过 traceroute ...

  9. Unity出现 error building player exception android (invocation failed)

    今天在编译Android的时候出现这个错误 error building player exception android (invocation failed) 百度谷歌之后,看到xuanyuson ...

  10. Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型

    原文 Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型 原文地址:Creating an Entity Framework Data Model for an ...