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

基本思路:

按照生成的思路写出相应代码,重点是考虑到循环何时跳出,此题若在

生成中间数的过程出现了重复,那一定会无休止的循环下去,便不是happy number,故用一个map来保存状态,暂未发现直接判断的方法

#define IMAX numeric_limits<int>::max()
class Solution {
public:
bool isHappy(int n) {
if(n<=0)return false;
map<int,int> vis;
//int num = pow(10,9);
while(n!=1)
{
// num++;
//if(num==IMAX)break;
if(vis.count(n))return false;//出现重复的中间数
vis[n]=1;
vector<int> vs;
while(n)
{
vs.push_back(n%10);
n=n/10;
}
n=0;
for(int i=0;i<vs.size();++i)n=n+vs[i]*vs[i];
}
return true;
}
};

19.Happy Number-Leetcode的更多相关文章

  1. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

  2. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

  3. Happy Number - LeetCode

    examination questions Write an algorithm to determine if a number is "happy". A happy numb ...

  4. Happy Number——LeetCode

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

  5. Letter Combinations of a Phone Number leetcode java

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  6. Super Ugly Number -- LeetCode

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  7. Largest Number——LeetCode

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. Letter Combinations of a Phone Number——LeetCode

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. Ugly Number leetcode java

    问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

  10. Single Number leetcode java

    问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...

随机推荐

  1. 详解DNS域名解析系统(域名、域名服务器[根、顶级、授权/权限、本地]、域名解析过程[递归与迭代])

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/105583806 学习课程:<2019王道考研计算机网络> 学习目的 ...

  2. 设计AOV网拓扑排序的算法

    拓扑排序 对一个有向图构造拓扑序列的过程称为拓扑排序(不唯一) 思想 从AOV网选择一个没有前驱的顶点并输出 从AOV网中删去该顶点,并且删去所有以该顶点为尾的弧 重复上述两步,直到全部顶点都被输出, ...

  3. 绑定socket描述符到一个网络设备

           网络编程中有时明明用eth0的地址来bind一个udp套接口, 可是发出去的包却是从eht1走的, 在网上找到这么一段话解释该问题:           在多 IP/网卡主机上,UDP ...

  4. 全面!总结BQ系列阻抗跟踪电量计化学Chemical ID配置和Golden学习方法

    BQ系列阻抗跟踪电量计SOC最高能达到1%,功能强大,应用起来也比较复杂.不仅要配置好参数,匹配好化学ID,并且进行好Golden学习和相关测试.本文就讲述ID匹配,Golden学习和测试的终极方法流 ...

  5. QuantumTunnel:Netty实现

    接上一篇文章内网穿透服务设计挖的坑,本篇来聊一下内网穿透的实现. 为了方便理解,我们先统一定义使用到的名词: UserClient:用户客户端,真实的请求发起方: UserServer:内网穿透-用户 ...

  6. jenkins 生成HTML报表,邮件推送

    1.登录jenkins,系统管理=>插件管理 =>可选插件安装 安装成功: 2.打开任务,进入配置 3.添加构建后操作 4.配置页面 5.构建后report输出配置完成后点击立即构建,构建 ...

  7. newInstance方法

    1.new 是java中的关键字,是创建一个新对象的关键字.用new这个关键字的话,是调用new指令创建一个对象,然后调用构造方法来初始化这个对象,如果反编译class的话,会看到一个Object o ...

  8. Python基础(@property)

    class Point(object): # def get_score(self): # return self.score # def set_score(self,value): # if no ...

  9. Databend 设计概述 | 白皮书

    Databend 是一个开源的.完全面向云架构的新式数仓,它提供快速的弹性扩展能力,并结合云的弹性.简单性和低成本,使 Data Cloud 构建变得更加容易. Databend 把数据存储在像 AW ...

  10. SpringCloud升级之路2020.0.x版-40. spock 单元测试封装的 WebClient(上)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们来测试下前面封装好的 WebClient,这里开始,我们使用 spock 编写 gro ...