- 浏览: 173844 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 229You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 343Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 335Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 452Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 514Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 431Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 622Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 534Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 862Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 556Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 765Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 630For a undirected graph with tre ...
相关推荐
LeetCode202_HappyNumber 判断一个数是否为happy nuumber,所谓happy number是指一个数,将其替换为其各位数字的平方和,重复这个过程,如果最终能得到1,这就是happy number;如果陷入一个不包含1的循环中,就不是...
高兴数的定义是 各个位上数的平方和等于1 比如100,10等
Homework 1. Helps to find happy number and consecutive numbers
Happy Number Ugly Number Ugly Number II Super Ugly Number Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse Integer Palindrome Number Insert Interval Merge Intervals ...
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) ...
判断是否为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)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 ...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 【链表】 实现单链表、循环链表、双向链表,支持增删操作 实现单链表...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!) 2、链表 实现单链表、循环链表、双向链表 支持增删操作 实现单链表反转 实现两个有序的链表合并为一个有序链表 ...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 链表 实现单链表、循环链表、双向链表,支持增删操作 实现单链表反转 ...
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)(选做)(注意:在第四天会进行继续学习) 练习: Three Sum(求三数之和) Majority Element(求众数) Missi