LeetCode之旅(18)-Happy Number
题目
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
思路
题目大意是要求将每个数字的平方相加,阵作为新的数字,一直计算下去,直到和为1,否则是会循环的。首先我把1~20的算了一下,发现没有总结出规律,不过不满足条件的数字是会循环出现,这样就可以判断如在等于一之前循环了,就不满足条件。(另外像这种平方的数字,不太好总结规律) 
代码
public class Solution {
     public int getHappy(int n){
        int sum =0;
        while(n!=0){
            sum += (n%10) * (n%10);
            n = n/10;
        }
        return sum;
    }
    public boolean isHappy(int n) {
        HashSet<Integer> happySet = new HashSet<Integer>();
        while(n!=1){
            if(happySet.contains(n))
                return false;
            happySet.add(n);
            n = getHappy(n);
        }
        return true;
    }
}
LeetCode之旅(18)-Happy Number的更多相关文章
- leetcode之旅(11)-Integer to Roman
		
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
 - LeetCode之旅(13)-Valid Anagram
		
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
 - LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
		
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
 - LeetCode之旅(17)-Ugly Number
		
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
 - leetCode之旅(14)-Number of 1 Bits
		
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
 - LeetCode 287. Find the Duplicate Number (找到重复的数字)
		
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
 - LeetCode之“排序”:Largest Number
		
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
 - 【leetcode】414. Third Maximum Number
		
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
 - LeetCode(137) Single Number II
		
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
 
随机推荐
- 海量数据处理算法(top K问题)
			
举例 有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M.返回频数最高的100个词. 思路 首先把文件分开 针对每个文件hash遍历,统计每个词语的频率 使用堆进 ...
 - 初次见面 你好EF
			
EF(yif),第一次听到这个名字的时候,以为是一个帅帅的魔术师,在小编的傻傻的梦想里,就是有一天,有一个魔术师站在小编面前,变出一大捧的玫瑰花,然后,然后不要钱`(*∩_∩*)′,然而在我们的编程世 ...
 - A*寻路算法入门(一)
			
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
 - mxgraph进阶(三)Web绘图——mxGraph项目实战(精华篇)
			
Web绘图--mxGraph项目实战(精华篇) 声明 本文部分内容所属论文现已发表,请慎重对待. 需求 由于小论文实验需求,需要实现根据用户日志提取出行为序列,然后根据行为序列生成有向图的形式 ...
 - JS中回调函数的写法
			
<!DOCTYPE HTML> <html><head> <meta charset="GBK" /><title>回 ...
 - TCP的定时器系列 — 超时重传定时器
			
主要内容:TCP定时器概述,超时重传定时器.ER延迟定时器.PTO定时器的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd Q:一条TCP连接会使用 ...
 - shell的date
			
使用方式 : date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [- ...
 - 【Android 系统开发】Android JNI 之 JNIEnv 解析
			
. jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/android-ndk-r9d/platforms/android ...
 - mixer: sql词法分析器设计
			
介绍 mixer希望在proxy这层就提供自定义路由,sql黑名单,防止sql注入攻击等功能,而这些的基石就在于将用户发上来的sql语句进行解析.也就是我最头大的词法分析和语法分析. 到现在为止,我只 ...
 - OJ题:将一个字符串顺序翻转
			
题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串. 之前写过这样的一个程序,用位运算的方法去操作指针,但是那样的方法未免就有点复杂啦,不如用以下这种,简单明了. 程序如下: #i ...