题目:

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^2 + 9^2 = 82
  • 8^2 + 2^2 = 68
  • 6^2 + 8^2 = 100
  • 1^2 + 0^2 + 0^2 = 1

链接:

https://leetcode.com/problems/happy-number/

答案:

暴力解决,直接给每个数的每位计算平方和就行,然后把中间的数都记录下来,如果下次出现这个数,就是loop了。

代码:

 #include<vector>

 using std::vector;

 class Solution {
private:
vector<int> med; bool inMed(int n)
{
vector<int>::iterator beg;
for(beg = med.begin(); beg != med.end(); ++ beg)
{
if(*beg == n)
{
return true;
}
} return false;
} int sum(int n)
{
int s = ;
while(n != )
{
int modn = n % ;
s += modn * modn;
n = n /;
} return s;
} public:
bool isHappy(int n)
{
if(n == || n == || n == || n == )
{
return true;
} med.clear();
med.push_back(n); int result = n;
bool isLoop = false;
while( result != )
{
result = sum(result);
if(result != && inMed(result))
{
isLoop = true;
break;
} med.push_back(result);
} return !isLoop;
}
};

代码中的isHappy()方法的最开头我是随便加了几个判断,哈哈,题目中给出的快乐数应该会在后台的测试数据中。

leetcode-【简单题】Happy Number的更多相关文章

  1. 乘风破浪:LeetCode真题_009_Palindrome Number

    乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...

  2. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  3. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  4. LeetCode算法题-Fibonacci Number(Java实现)

    这是悦乐书的第250次更新,第263篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第117题(顺位题号是509).Fibonacci数字,通常表示为F(n),形成一个称为 ...

  5. LeetCode算法题-Perfect Number(Java实现)

    这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...

  6. Go: LeetCode简单题,简单做(sort.Search)

    前言 正值端午佳节,LeetCode也很懂.这两天都是简单题,早点做完去包粽子. 故事从一道简单题说起 第一个错误的版本 简单题 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最 ...

  7. LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)

    这是悦乐书的第311次更新,第332篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第180题(顺位题号是762).给定两个正整数L和R,在[L,R]范围内,计算每个整数的 ...

  8. LeetCode算法题-Largest Number At Least Twice of Others(Java实现)

    这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...

  9. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  10. LeetCode算法题-Guess Number Higher or Lower(Java实现)

    这是悦乐书的第211次更新,第224篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第79题(顺位题号是374).我们正在玩数字游戏. 游戏如下:我从1到n中选择一个数字. ...

随机推荐

  1. 初涉hash

    今天和朋友讨论一个问题 有两百亿个数,我给出一个数,找到这两百亿个数中两数相加等于它的组合.要求时间复杂度为线性,空间2G 解决思路是开一个hash表比如a[1000]将所有数存入hash表中,a[i ...

  2. Static List

    Static ListStatic List is the smart implementation of list data structure for those languages that h ...

  3. MongoDB学习笔记九:分片

    分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程.有事也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机既可以存储更多的数据 ...

  4. C# 创建开机启动核心代码

    #region 访问 AppSettings private void SaveAppSetting(string key, string value) { var config = Configur ...

  5. Oracle索引失效问题:WHERE C1='' OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待

    问题描述: 项目反馈某功能响应时间很长,高峰期时系统整体响应很慢... 获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长, ...

  6. sqlserver和Oracle内部的错误数据修复(DBCC、DBMS_REPAIR)

    数据库长时间运行后,因断电.操作系统.物理存储等的原因可能会造成数据库内部的逻辑或物理错误,我们可以使用一般的方式尝试修复. 对于sqlserver 我们可以使用DBCC命令: -- sqlserve ...

  7. symfony2取得web目录绝对路径、相对路径、网址的函数是什么

    对于你的需求,Symfony2通过DIC提供了kernel服务,以及request(请求)的封装. 在controller里(在其他地方你可以自行注入kernel,这个服务是HttpKernel库里的 ...

  8. UICollectionView的使用

    感谢下面文章作者的无私奉献 基础用法: http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html http://my.oschina.net/jo ...

  9. Deployment failure on Tomcat 6.x. Could not copy all resources to……

    http://blog.knowsky.com/194238.htm tomcat部署不成功 Deployment failure on Tomcat 6.x. Could not copy all ...

  10. ruby 生成有条件限制的随机数

    #conding:utf-8 #生成只有数字的随机码可控制长度def random_int(len) newpass = "" 1.upto(len){ |i| newpass & ...