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. Object-c中的属性和成员变量的关系详解

    很多人在初学移动开发的时候会对object-c中的合成存取方法感到疑惑,此处尝试为看到本文有缘人答疑解惑,鄙人才疏学浅,难免有疏漏谬误之处,热烈欢迎诸位看官拍砖指点. 1.合成存取方法: OC为增加开 ...

  2. HBase-再看HBase

    为什么HBase主要应用于在线应用系统?(还没搞懂) www.github.com/HBaseinaction google 三大论文  Google File System MapReduce Bi ...

  3. org.apache.hadoop.fs-ChecksumException

    当ChecksumFileSystem出现问题时抛出 package org.apache.hadoop.fs; import java.io.IOException; /** Thrown for ...

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

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

  5. Android快速开发框架ZBLibrary源码分享

    坐标标准库ZBLibrary,是一个MVP架构的Android快速开发框架,提供一套开发标准(UI,Data,Listener)以及模板和工具类并规范代码. 封装层级少,简单高效兼容性好.Androi ...

  6. javaweb学习总结二十一(servlet开发入门、servlet生命周期以及调用过程)

    一:servlet开发入门 servlet是sun公司一门开发动态web资源的技术,下面编写一个servlet入门程序: 1:在tomcat服务器webapps目录下新建firstServlet目录, ...

  7. Linux下进程的创建

    这篇文章主要是讲解到Linux进程的控制,包括程序和进程.守护进程.守护进程的出错处理. 1.程序和进程 程序(program)是存放在磁盘文件中的可执行文件,程序的执行实例被称为进程(process ...

  8. Delphi版本号对照(转)

    Delphi版本号对照 VER10  :Turbo Pascal 1VER20  : Turbo Pascal 2VER30  : Turbo Pascal 3VER40  : Turbo Pasca ...

  9. AngularJS Boostrap Pagination Sample

    首先,样式是这样的 首先,Service端是Webapi REST JSON格式 第二,我们建立一个Wrapper Class,这里你也可以定义一个Generic<T>,作为示例,我们这里 ...

  10. Apache httpd开启SSL

    https://my.oschina.net/xpbug/blog/197454 必要软件 apache httpd 2.4(windows) perl 5 (windows) openssl (wi ...