package isHappy202;
/*
* 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.
*/ public class Solution {
/*
* solution:
* 1
* 2-4-16-37-58-89-145-42-20-4
* 3-9-81-65-61
* 4
* 5-25-29-85-89
* 6-36-45-41-17-50
* 7-49-97-130-10
* 8-64-52-29
* 9-81-65
* so only 1 and 7 is "happy"
*/
public static boolean isHappy(int n) {
while(n/10>0){
int sum=0;
while(n/10>0){
sum+=Math.pow((n%10),2);
n=n/10;
}
sum+=Math.pow(n,2);
n=sum;
}
if (n==1||n==7)
return true;
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(isHappy(23456));
}
}

LeetCode----202. Happy Number(Java)的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  4. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  7. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. LeetCode 5108. Encode Number - Java - 2进制

    题目链接:https://leetcode-cn.com/problems/encode-number/ Given a non-negative integer num, Return its en ...

  10. leetcode 136. Single Number ----- java

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

随机推荐

  1. JSP + AJAX完整实例及代码

    (1)发送请求index.jsp,注意引入jquery.min.js文件 <%@ page language="java" contentType="text/ht ...

  2. DBA日常工作内容和职责

    1.统计数据库总容量 按表空间分别统计: 总容量(单位为M): 2.计算用户下所有对象大小 3计算USERS表空间的大小 4计算该USERS表空间对象大小 ---------------------- ...

  3. Jquery中animate可以操作css样式属性总结

    可以用 animate() 方法来操作所有 CSS 属性吗? 是的,几乎可以!不过,需要记住一件重要的事情:当使用 animate()时, 必须使用 Camel 标记法书写所有的属性名,比如,必须使用 ...

  4. SpringMVC如何接收application/json内容编码类型的参数?

    在上代码之前,有必要先说说@ResquestBody注解的含义: 1.官方解释如下: Annotation indicating a method parameter should be bound ...

  5. js的extend和fn.extend使用

    $.fn.extend扩展的是一个jQuery对象函数,$.extend扩展的是一个jQuery全局函数 <!DOCTYPE html> <html> <head> ...

  6. js 将一段html插入到body最前面

    var wrap=document.createElement("div"); var first=document.body.firstChild;//得到页面的第一个元素 va ...

  7. 形形色色的下拉菜单 (css3)

    http://www.iteye.com/news/25339

  8. Android课程---Oracle VM VirtualBox出现不能为虚拟机打开一个新任务

    因工作需要在Win7下增添了Win7虚拟系统,随着VirtualBox 4.326的版本更新,用户们也开始升级.一用户在升级后发现原来创建的虚拟机无法打开,提示信息为:不能为虚拟电脑win7打开一个新 ...

  9. 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解

    Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解   多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...

  10. Thinking in Java——笔记(1)

    Introduction To Obejct The progress of abstraction But their primary abstraction still requires you ...