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. CSP-S 2021 遗言

    感谢€€£,谢谢宁嘞! 第一题,€€£给了很多限制条件,什么"先到先得"."只有一个跑道",让它看起来很好做,然后来骗,来偷袭,广大"消费者" ...

  2. openmp学习心得(一)

    主要在vs2015下使用OMP,写一些自己omp的学习心得: 一.在VS2015下OpenMP的使用: 1.VS2015也仅仅支持OpenMP2.0版本,VS对OpenMP的支持并不太好. 2.在VS ...

  3. Spring IOC(控制反转)和DI(依赖注入)原理

    一.Spring IoC容器和bean简介 Spring Framework实现了控制反转(IoC)原理,IoC也称为依赖注入(DI). 这是一个过程,通过这个过程,对象定义它们的依赖关系,即它们使用 ...

  4. 0x04

    二分: while(l<r) { int mid=(l+r)/2; if(符合条件) r=mid; else l=mid+1; } 固定下二分的写法: 终止条件:l==r: 取mid=(l+r) ...

  5. Ubuntu中python的mysql操作

    1.在已经安装了python和MySQL数据库的前提下使用pip3 install PyMySQL命令 2. 建立链接: (1)首先使用命令python 进入编程模式,再导入包: import pym ...

  6. k8s入坑之路(14)scheduler调度 kubelet管理及健康检查 更新策略

    kubelet 主要功能 Pod 管理 在 kubernetes 的设计中,最基本的管理单位是 pod,而不是 container.pod 是 kubernetes 在容器上的一层封装,由一组运行在同 ...

  7. 【浏览器】聊聊DOM

    [浏览器]聊聊DOM 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 作为前端开发,在以前的工作中大多是和DOM打交道,到 ...

  8. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

  9. 《Python语言程序设计》【第3周】基本数据类型

    实例3:天天向上的力量 #DayDayUpQ1.py dayup = pow(1.001,365) daydown = pow(0.999,365) print("向上: {:.2f},向下 ...

  10. 通过python来获取网页状态

    #!/usr/bin/python import sys,httplibfrom optparse import OptionParserusageString = "Usage: %pro ...