- 浏览: 184891 次
- 性别:
- 来自: 济南
文章分类
最新评论
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
题目中规定一个happy number的规则,我们借助于哈希表存储已经出现过的数字,如果计算的结果为1我们就返回true;如果计算出的结果不为1并且哈希表中已经存在这个数,就说明了它无限循环,我们返回false。代码如下:
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
题目中规定一个happy number的规则,我们借助于哈希表存储已经出现过的数字,如果计算的结果为1我们就返回true;如果计算出的结果不为1并且哈希表中已经存在这个数,就说明了它无限循环,我们返回false。代码如下:
public class Solution { public boolean isHappy(int n) { HashSet<Integer> set = new HashSet<Integer>(); int tem = n; while(true) { if(tem == 1) return true; if(set.contains(tem)) { return false; } else { set.add(tem); tem = getSquare(tem); } } } public int getSquare(int n) { int result = 0; while(n > 0) { result += (n % 10) * (n % 10); n /= 10; } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
在编程领域,我们经常遇到各种有趣的数学问题,"高兴数"(Happy Number)就是其中之一。高兴数是指这样一个整数,它的各位数字的平方和等于1。例如,100和10都是高兴数,因为1² + 0² + 0² = 1,而1² + 0² = 1。...
LeetCode202_HappyNumber 判断一个数是否为happy nuumber,所谓happy number是指一个数,将其替换为其各位数字的平方和,重复这个过程,如果最终能得到1,这就是happy number;如果陷入一个不包含1的循环中,就不是...
”表明这是一个关于编程作业的压缩包,重点在于实现找到快乐数(Happy Number)和连续数的功能。快乐数是指在数位平方和的迭代过程中最终达到1的数字,而连续数是指差为1的两个整数。这个作业可能涉及到基础的算法...
public class HappyNumber { public static boolean isHappy(int n) { int slow = n; int fast = getNext(n); while (fast != 1 && slow != fast) { slow = getNext(slow); fast = getNext(getNext(fast)); }...
1. **Happy Number**:编写程序找出1000以内所有"Happy Number",即一个数等于其所有因子之和。可以通过循环计算每个数的因子之和,如果满足条件则输出。 2. **分式求和**:要求计算特定公式的前20项之和,并保留6...
python python_leetcode题解之202_Happy_Number.py
本压缩包文件"python-leetcode面试题解之第202题快乐数-题解.zip"显然是一个针对LeetCode第202题“快乐数”(Happy Number)的Python解决方案集。 快乐数是一个有趣的数学概念,它与数字的算术性质有关。在这个问题...
10. **202.py** - 最后是202题,"Happy Number"(快乐数),这是一个简单的数学问题,通过迭代检查数字的平方和是否最终会达到1来判断一个数是否为快乐数。 每一道题目都可能包含多种解题策略,如动态规划、贪心...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
- Happy Number:一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,也可能是无限循环但始终不会为1。如果可以变为1,那么这个数就是快乐数...
- **快乐数(Happy Number)**: 判断一个数字是否快乐。 - **二进制中的1的个数(Count 1 in Binary)**: 计算一个整数的二进制表示中有多少个1。 #### 动态规划 - **斐波那契数列(Fibonacci)**: 一个经典的动态规划...
判断是否为HappyNumber,输入一个数,将该数的各个位数进行平方,然后求和,再取各个位数进行平方,再求和,直到为1(此时平方求和不再改变),则为HappyNumber。如果结果是某些无限循环的非1的值,则不是Happy...
HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 ...
项目树 . ├── List.sh ├── README.md ├── Readme.sh ├── _Algorithm │ └── src ...│ │ ├── _202_HappyNumber.java │ │ ├── _279_PerfectSquares.java │ │ ├── _2
04-23从Alphabet到IntegerMapping的DecryptString 2020-04-25合并两个已排序的NodeList Fibonacci 2020-04-26查找ContinuousSequence MaximunNum 2020-04-28递归多态2020-04-30 HappyNumber 2020-05-01 AddBinary ...
leetcode 不会准备工作 :puzzle_piece: 用于通过测试驱动开发解决 LeetCode 问题的 TypeScript ...Happy Number ' 关于这个脚本这个 repo 带有一个脚本,可以为新的 LeetCode 问题快速设置启动文件
happyNumber array 155 minStack stack 111 二叉树的最小深度 , tree 110 平衡二叉树 tree 104 二叉树的最大深度 tree 100 相同的树 tree 94 二叉树的中序遍历 tree 70 爬楼梯 dp 53 最大子序和 dp 26 删除排序数组...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)! 2. 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 实现两个有序的链表合并为一个有序链表 实现求链表的中间结点 附加...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...