博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode/LintCode] Happy Number
阅读量:7219 次
发布时间:2019-06-29

本文共 948 字,大约阅读时间需要 3 分钟。

Problem

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

1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

Solution

updated 2018-9

class Solution {    public boolean isHappy(int n) {        Set
record = new HashSet<>(); while (n != 1) { if (record.contains(n)) return false; record.add(n); int sum = 0; while (n != 0) { //calculate sum sum += Math.pow(n%10, 2); n /= 10; } n = sum; } return true; }}

转载地址:http://mmxym.baihongyu.com/

你可能感兴趣的文章
chapter 4:贪心
查看>>
批处理学习笔记
查看>>
Linux挂载磁盘
查看>>
Cyclone II RAM ROM设置
查看>>
Ubuntu下实现伪静态
查看>>
python 二维数组遍历
查看>>
第8周课下作业1(补)
查看>>
阿萨斯
查看>>
service启动和停止,绑定和解除绑定
查看>>
elasticsearch开机启动脚本
查看>>
window service 恢复选项卡设置
查看>>
车辆管理系统之编码过程总结(十一)
查看>>
基于AOE网的关键路径的求解
查看>>
2017-5-16 python标准库
查看>>
浅谈游戏的声音处理-流播放文件 source
查看>>
旧版本转换成支持ARC版本
查看>>
创建与服务器的输入输出流
查看>>
string.hのmemmove的实现
查看>>
dicom网络通讯入门(1)
查看>>
日常训练.jpg
查看>>