LeetCode----202. Happy Number(Java)
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)的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 5108. Encode Number - Java - 2进制
题目链接:https://leetcode-cn.com/problems/encode-number/ Given a non-negative integer num, Return its en ...
- leetcode 136. Single Number ----- java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
随机推荐
- MyEclipse安装lombok
1. 将lombok.jar复制到myeclipse.ini所在的文件夹 2. 打开myeclipse.ini,插入以下两行: -Xbootclasspath/a:lombok.jar-javaage ...
- IOS第一天多线程-03线程间通信
**** #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomi ...
- C# 常用类
一.Convert 主要用于数据类型的转换,常用的静态方法有: Convert.ToSingle():把数据转换为单精度浮点数,参数常为字符串 Convert.ToDouble():转为双精度浮点数 ...
- 【iCore3 双核心板】例程十:RTC实时时钟实验——显示日期和时间
实验指导书及代码包下载: http://pan.baidu.com/s/1jHuZcnc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- css:使用笔记(不断更新中...)
1,inline-block Inline :1是不支持宽高的 2 在一行内显示 span 啥的~ Block:1,占一行 2支持宽高 但是img因为在一行并排显示,我们归为行(内嵌)元素,但支持设置 ...
- Qt常用命令收集
qt的命令很多,用到的时候到网上查,常常不能一下查到.这里记录下一些备用 1 从.ui文件生成头文件: uic xxx.ui > xxx.h 2 moc生成 moc yourfilename.h ...
- android 一个简单的服务例子
public class MessageService extends Service { // 获取消息线程 private MessageThread messageThread = null; ...
- 微信蓝牙BLE接入调试指引 硬件篇
1 平台框架简介 微信蓝牙BLE由三个模块组成,分别是蓝牙设备.微信和第三方服务器,如下图: 蓝牙设备与微信之间的通信是通过蓝牙GATT协议进行. 微信与第三方服器之间的通信是通过网络http 接口进 ...
- Xcode 杂七杂八
一.Exception 的捕捉 1.message send to dealloc instance a, 输出控制台(lldb)后面输入:c + enter, 找到对应的行 b, po ...
- java调用 webservices接口实现天气预报
最近要用到web services,而这两天也比较有空,就弄了一个获取天气预报的Util.以前以为这有多难,因为数据来源是个困难.现在用web services的技术,发现下面这个是相当不错的.下面就 ...