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 ...
随机推荐
- 自动滚动的TextView
自动滚动的TextView 效果图 XML文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr ...
- 设置TextView显示的文字可以复制
设置TextView显示的文字可以复制 效果图 在xml中设置 <TextView android:layout_width="wrap_content" android:l ...
- Shell命令:echo 命令详解
http://blog.chinaunix.net/uid-27124799-id-3383327.html # echo命令介绍 功能说明:显示文字. 语 法:echo [-ne][字符串] / e ...
- Dynamics CRM 后台通过组织服务获取时间字段值的准确转换
做CRM开发的都知道,在系统时间字段的处理上是有讲究的,因为数据库中存的是UTC时间,CRM的界面时间字段会根据个人设置中的时区以及格式自动调整,这是最基本的一面,那还有很多使用时间的场景,比如脚本使 ...
- (NO.00005)iOS实现炸弹人游戏(八):游戏主角(一)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 最近一直在做另一个RPG游戏,所以本系列迟迟没有更新,上一篇博 ...
- CMake搜索Boost1.57失败及解决
CMake更新到3.1.0,Boost更新到1.57,结果CMake搜索Boost失败: Unable to find the Boost header files. Please set BOOS ...
- pig里面没有if:不能判断一个条件后决定一个执行步骤
pig是处理流 的工具,所以数据集是流对象,处理步骤也是一样的. Pig中存在按条件处理流对象的方式有 1)filter X= FILTER A BY (f1 == 8); 2)CASE WHEN T ...
- hive编程指南——读书笔记(无知拾遗)
set hive.metastore.warehouse.dir=/user/myname/hive/warehouse; 用户设定自己的数据仓库目录.不影响其他用户.也在$HOME/.hiverc中 ...
- Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(七)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 打开SpriteBuilder,在文件视图中新建一个文件夹Fon ...
- 今日成为CSDN认证专家
认证时写的申请材料: 程序猿一枚毕业于南开工作于上海.喜欢读书,喜欢跑步,激情似火,心静如水. 喜欢编程,喜欢寻根问底各种技术,喜欢在各种新技术中汲取营养. 喜欢分享,因此以一些高质量的博文来回报各位 ...