leetcode: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
- 1*1 + 9*9 = 82
- 8*8 + 2*2= 68
- 6*6 + 8*8= 100
- 1*1 + 0 + 0= 1
题目要求对任意一个正整数,不断计算各个数位上数字的平方和,若最终收敛为1,则该数字为happy number,否则程序可能从某个数开始陷入循环。这道题目我们只用根据规则进行计算,并使用图来存储已经出现过的数字即可。
class Solution {
public:
unordered_map<int, int> path;//使用图来存储每个计算结果
bool isHappy(int n) {
int result=0;
int key=n;
while (n) {//按照规则计算,计算key的result
int temp = n%10;
result+=temp*temp;
n/=10;
}
if (result==1) {//如果result为1,则原正整数为happy number
return true;
}
if (path.find(result)!=path.end()) {//如果result已经在图中,即存在循环,所以key不是happy number
return false;
}
path[key]=result;//将key或result存储到图中
return isHappy(result);//递归计算
}
};
当然解决方案还是有很多的,其中有很多是采用哈希表的递归方案:
class Solution {
public:
unordered_set<int> check;
bool isHappy(int n) {
string tmp = to_string(n);
int count = 0;
for(char each:tmp){
count += (each-48)*(each-48);
}
if(check.count(count) == 0)
check.insert(count);
else
return false;
return count == 1? true : isHappy(count);
}
};
其他解法:
运行时间:8ms
class Solution {
public:
bool isHappy(int n) {
map<int,int> temp;
while(true){
if(n==1)
return true;
if(temp[n]==1)
return false;
temp[n]++;
n = Caculate(n);
}
}
int Caculate(int n){
int ret=0;
while(n!=0){
ret+=(n%10)*(n%10);
n=n/10;
}
return ret;
}
};
不过在本题中使用unordered_set还是要更好的,因为图中你的算法有插入和查找的时间。而且我们不是必须得存储计算结果(values),只需要知道是否看到过这个数字而已。
所以改进方案:
(运行时间:4ms)
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> s;
while(true){
if(n==1)
return true;
if(s.find(n)!=s.end())
return false;
s.insert(n);
n = Caculate(n);
}
}
int Caculate(int n){
int ret=0;
while(n!=0){
ret+=(n%10)*(n%10);
n=n/10;
}
return ret;
}
};
leetcode:Happy Number的更多相关文章
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- leetcode:Palindrome Number
Question: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Cou ...
- leetcode:Single Number
public int SingleNumber(int[] nums) { if(nums==null||nums.Length%2==0) return 0; int ret=nums[0]; fo ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
随机推荐
- highchart 导出图片, 显示空白
使用highchart时, 导出的图片会变空白.. 解决方案: 不要加载grid.js
- Kali Linux 命令集
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- 使用fwrite()函数和fprintf()函数输出数据到文件时的区别
使用书上的一个课后题为例 有5个学生,每个学生有3门课的成绩,从键盘输入学生数据(包括学号,姓名,3们课程成绩),计算出每个学生的平均成绩,将原有数据和计算出的平均分数存放在磁盘文件“stud”中. ...
- 关于JS APP
多屏screen, JS如何路由,如何换页,导航.通过JS来实现. 当前页面的逻辑通过JS来实现.HTML DOM, Event, Widget. 核心在于function. JS 不仅仅是DOM, ...
- SQL TRY CATCH
begin try select 1/0end trybegin catch select error_number() as 'number', error_line() as 'line', er ...
- iOS获取手机相关信息
iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- (转)价值240万的photoshop中文教程,错过了后悔都来不及 (吹得好响)
PS抠图方法 一.魔术棒法——最直观的方法 适用范围:图像和背景色色差明显,背景色单一,图像边界清晰. 方法意图:通过删除背景色来获取图像. 方法缺陷:对散乱的毛发没有用. 使用方法:1.点击“魔 ...
- poj 3072(最短路)
题目链接:http://poj.org/problem?id=3072 一涉及稍微计算几何方面的东西就要做好久,一开始先用SPFA写的,可能是由于松弛次数过多导致精度损失,郁闷了好久,然后改成Dijk ...
- HTML基本操作
插入图片: 1.利用链接(静态) <img src="http://www.kmwzjs.com/useruploads/images/20101020_057600100825157 ...