leetcode_202题——Happy Number(哈希)

Happy Number

Total Accepted: 7315 Total Submissions: 22724My Submissions

Question Solution

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

Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

Hide Tags

Hash Table Math

Have you met this question in a real interview?

Yes

No

Discuss

这道题将每次根据前一个值计算出来的结果储存在set中,再进行查找就可以了

#include<iostream>
//#include<map>
#include<set>
using namespace std;

int n_to_n(int a)
{
	int b=a;
	int last_result=0;

	while(b!=0)
	{
		last_result=(b%10)*(b%10)+last_result;
		b=b/10;
	}
	return last_result;
}

bool isHappy(int n) {
	set<int> temp;
	temp.insert(n);
	int a=n;
	bool c;
	while(1)
	{
		int b=n_to_n(a);
		if(b==1)
		{
			c=1;
			break;
		}
		if(temp.count(b)==1)
		{
			c=0;
			break;
		}
		temp.insert(b);
		cout<<b<<endl;
		a=b;
	}
	return c;
}
int main()
{
	cout<<isHappy(2)<<endl;
}

  

时间: 2024-10-10 00:08:02

leetcode_202题——Happy Number(哈希)的相关文章

leetcode_136题——Single Number(哈希表hashtable,multiset)

#include<iostream> //#include<bitset> //#include<map> #include<set> using namespace std; /*这道题,直接采用multiset来做,就太简单了,没啥好说的,就是全导进去,然后count下 就OK了,因为在set中查找都O(1)所以呢是线性的算法复杂度 */ int singleNumber(int A[], int n) { multiset<int> tem

湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighb

Leetcode第九题_Palindrome Number

Palindrome Number Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However, if you have solved th

leetcode_9题——Palindrome Number (数学问题)

Palindrome Number Total Accepted: 57795 Total Submissions: 194286My Submissions Question Solution Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Hide Tags Math Have you met this question in a real i

(模板题)Number Sequence -- Hdu -- 1711

http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16080    Accepted Submission(s): 7100 Problem Description Given two sequences of n

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

leetcode第九题--Palindrome Number

Problem: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of

leetcode第136题-Single Number

题目要求:给出一个数组,只有一个数字出现一次,其他的都出现两次,找出那出现一次的数字,要求用线性的时间解出题目! 分析:因为题目要求的是用线性时间,所以类似于那种暴力解决的方法会超时,如下面这种: int singleNumber2(int *nums,int n) { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(nums[i]==nums[j]) break; else continue; } if(j==n) return num

leetcode第202题-Happy Number

说实话,如果不看别人的解答的话,这道题我也是没有思路,不知道该循环几次,也不知道循环的终止条件,后来才知道,[2-6]这个范围内的数字都不是happy number 所以就有了终止条件,n>6就是终止条件,当n跳进这个范围内的时候就终止循环,最后就能判断是否是happy number了 #include<stdio.h> #include<stdlib.h> bool isHappy(int n) { while(n>6) { int sum=0; while(n) {