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 ...
随机推荐
- 查看windows系统热键占用情况
有时候我们经常用一些软件中的快捷键,但是会发现快捷键设置的很正确,但是就是不起作用.这就是因为这些快捷键被系统或者其他软件占用了. 那么这时我们怎么知道是哪个软件占用了呢?这确实是个纠结的问题,还好大 ...
- 不定义JQuery插件,不要说会JQuery
转自:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#") ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- Android图片缩放方法
安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...
- HDU 5151 Sit sit sit 区间DP + 排列组合
Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...
- C# 使用WIN32API设置外部程序窗口无边框
使用代码 var wnd = win32.FindWindowA(null, "窗口标题"); Int32 wndStyle = win32.GetWindowLong(wnd, ...
- Linux command: usermod -- 改变用户状态
应用举例: 1. usermod -g newuser newuser force use GROUP as new primary group. 一般时候是默认的,也是必须的(不能更改).2. 指定 ...
- java中的基本数据类型存放位置
基本数据类型是放在栈中还是放在堆中,这取决于基本类型声明的位置. 一:在方法中声明的变量,即该变量是局部变量,每当程序调用方法时,系统都会为该方法建立一个方法栈,其所在方法中声明的变量就放在方法栈中, ...
- ios开发--第三方整理
一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...
- Emmet语法介绍
例子: (div+p#test>span.test2.test3)*5+p[name="hello"]>div.test4^a*5 <div></di ...