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

思路:把一个数每一位平方后加和得到一个新的数,然后继续对这个新的数做同样的操作。如果最后能得到一个1,则最原始的数就是一个”Happy Number”。如果在还没达到1的过程中出现循环,则这个数肯定不是。用到C++ 的set。set.count()判断一个数是否在set中,set.insert(),向set 中插入元素。INT_MAX,表示最大整数。

class Solution {
public:
bool isHappy(int n) {
if(n==)
return false;
set<long> s;
while(n<=INT_MAX)
{
if(n==)
return true;
if(s.count(n)>)
return false;
s.insert(n);
n=digitSquare(n);
}
return false; }
long digitSquare(long num)
{
long r=;
int t=;
while(num)
{
t=num%;
r+=t*t;
num/=;
}
return r;
}
};

40. leetcode 202. Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  5. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  8. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

随机推荐

  1. jQuery Ajax封装(附带加载提示和请求结果提示模版)

    1.创建HTML文件(demo) <!doctype html> <html lang="en"> <head> <meta charse ...

  2. MySQL,Oracle,PostgreSQL 数据库web维护客户端管理工具

    TreeDMS数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL 等数据库进行维护管理操作. 功能包 ...

  3. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  4. AngularJS-repeat指令

    <body ng-app="myApp"> <div ng-controller="myCtrl"> <ul> <li ...

  5. 精通 JS正则表达式(转)

    转载的目的在于:增加一些自己看不懂的解释.内容只加不改,灰色字体是自己寻找并增加的. 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式 ...

  6. h5可预览 图片ajax上传 (补更),后台数据获取方法---php

    原理是 先获取,然后手动转移文件路径,不然会被服务器自动删除 demo如下: <?php header('content-Type:text/html;charset=utf-8'); $fil ...

  7. .net 实现aop的三种方法。

    动态代理 透明代理 编译时注入

  8. Java IO设计模式(装饰模式与适配器模式)

    01. 装饰模式 1. 定义 Decorator装饰器,就是动态地给一个对象添加一些额外的职责,动态扩展,和下面继承(静态扩展)的比较.因此,装饰器模式具有如下的特征: 它必须持有一个被装饰的对象(作 ...

  9. str-字符串功能介绍

    叨逼叨:字符串的各个功能修改不是本身,本身不变,会产生新的值,需要赋值给新的变量来接收 以下 "举例" 是解释每个功能的实例   "举例"下一行是pycharm ...

  10. hdu_1564: Play a game

    题目链接 看n的奇偶性,题解参见kuangbin的博客 http://www.cnblogs.com/kuangbin/archive/2013/07/22/3204654.html #include ...