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. Git基本命令行操作

    A. 新建Git仓库,创建新文件夹git init  B. 添加文件到git索引git add <filename>  --- 单个文件添加git add * --- 全部文件添加 C. ...

  2. hdu Hike on a Graph

    此题是道bfs搜索的题目.bfs的精髓就是找到下一步的所有可能然后存储起来,有点暴力的感觉,这题就是每步中 所有的可能都入队,然后一一 判断.这道题的题意是 : 给你一幅完全图,再给你三个盘,目的是把 ...

  3. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  4. jQuery还原select下拉列表和清空input的值,回显下拉列表框的值

    实现用jQuery还原select下拉列表的值,用了很多种方式,花了一些时间,最后重要找到一种可以实现的方式, 页面上有这些内容 <select id ="level" na ...

  5. 字符串—strcpy

    来自——百度百科   原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #include ...

  6. NLP文本情感分类传统模型+深度学习(demo)

    文本情感分类: 文本情感分类(一):传统模型 摘自:http://spaces.ac.cn/index.php/archives/3360/ 测试句子:工信处女干事每月经过下属科室都要亲口交代24口交 ...

  7. BizTalk开发系列(五) 属性字段

    在根据消息内容进行路由的时候经常使用的是可分辨字段和属性字段.属性字段可以在各个 BizTalk Server 组件(包括管道和业务流程)中进行访问.属性字段还可用于消息路由.如果需要在上下文(而不是 ...

  8. 【iCore3双核心板】iCore3双核心板使用说明(图文)

    1.iCore3供电.程序下载线路连接示意图(使用iTool2) 2.iCore3供电.程序下载线路连接示意图(使用J-link和Blaster) 3.iCore3供电.读U盘线路连接示意图

  9. MVC4脚本压缩 BundleTable bundles 404错误

    在发布网站的时候,因为使用了MVC4的新特性BundleTable,造成访问的时候js和css报了404错误, google了以后, 有朋友说是因为要在webservice添加 <modules ...

  10. Splay树-Codevs 1296 营业额统计

    Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...