Happy Number——LeetCode
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
LeetCode新题,题目大意:给定一个数,将其每一位平方和相加得到一个新的数,以此循环往复,最后得到的数如果是1,则给定的数是happy number,否则会无尽的循环。
解题思路:一开始直接模拟,循环100次还不是1直接剪掉return false,也能AC,但是毕竟可能有情况覆盖不到,改用HastSet存放中间数就行了,如果得到的新结果在中间数集合中出现了,那么一定会陷入循环并且得到的不是1。
Talk is cheap>>
public boolean isHappy(int n) {
Set<Integer> res = new HashSet<>();
int sum = n;
while (sum != 1) {
n = sum;
sum = 0;
while (n != 0) {
sum += (n % 10) * (n % 10);
n /= 10;
}
if (res.contains(sum)) {
return false;
}
res.add(sum);
}
return true;
}
Happy Number——LeetCode的更多相关文章
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- Happy Number - LeetCode
examination questions Write an algorithm to determine if a number is "happy". A happy numb ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Largest Number——LeetCode
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Letter Combinations of a Phone Number——LeetCode
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
随机推荐
- HTML5 canvas createRadialGradient()放射状/圆形渐变
定义和用法 createLinearGradient() 方法创建放射状/圆形渐变对象. 渐变可用于填充矩形.圆形.线条.文本等等. 提示:请使用该对象作为 strokeStyle 或 fillSty ...
- Datum Form Goole Android
1. <TurboChargeYourUI-How to make your AndroidUI fast and efficient> 2. <The World of List ...
- SDK Manager.exe 无法启动,一闪而过的解决办法
删掉 C:\Windows\system32\ 下的 java.exe.javaw.exe.javaws.exe 即可解决.(转载)
- iOS 原生二维码扫描(可限制扫描区域)
篇文章的主要原因不是展示如何使用 AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围.(因为默认的是全屏扫描) 项目遇到扫描二维码的功能需求,这里我放弃了使用三方库,而采用了 ...
- JAVA开发环境 - 环境变量及配置
JDK是什么?JRE是什么? JRE(Java Runtime Environment):Java运行环境: JDK(Java Development Kit):Java开发工具包,里面已经包含JRE ...
- X3850 Linux 下DSA日志收集办法
收集工具下载 RHEL 6: 32bit-- [IBM 下载]http://delivery04.dhe.ibm.com/sar/CMA/XSA/03tza/1/ibm_utl_dsa_dsytb7x ...
- Linux下su与su -命令的区别
在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用service命令了. 原因: su命令和su -命令 ...
- X-Plane飞行模拟器购买安装
要玩起X-Plane第一个步骤当然是购买了,要购买其实非常简单,只需要一张能够支持MasterCard或者其他外币结算的信用卡,在http://www.x-plane.com/官网上购买即可,比逛淘宝 ...
- sicily-2499 平方数
题目分析: 一个数可以表示成四种状态,所以可以用一个状态数组来存放该数由几个数的平方和表示.1.表示该数本身是完全平方.2.表示该数是由两个平方和3.表示三个.4.表示4个.一次遍历找出本身是完全平方 ...
- 3 - testng.xml
TestNG的调用有以下几种方式: testng.xml ant 命令行 这部分主要介绍testng.xml的格式. 当前testng.xml的DTD(文档类型定义(Document Type Def ...