19.Happy Number-Leetcode
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的更多相关文章
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- Happy Number - LeetCode
examination questions Write an algorithm to determine if a number is "happy". A happy numb ...
- Happy Number——LeetCode
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Super Ugly Number -- LeetCode
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Largest Number——LeetCode
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Letter Combinations of a Phone Number——LeetCode
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
随机推荐
- Linux下Zabbix5.0 LTS监控基础原理及安装部署(图文教程)
Zabbix 是什么? zabbix 是一个基于 Web 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.通过 C/S 模式采集数据,通过 B/S 模式在 Web 端展示和配置,能监视 ...
- 字符串折叠&压缩(区间DP)
字符串折叠 题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS-S(X个S). 如果A = A ...
- C#笔记1__命名空间 / 常量 / object / is、as、...?... :...
命名空间:namespace Test1{ ... } 引用命名空间:using System; using 别名=命名空间 常量:const double PI=3.14; using System ...
- Java 中 List 分片的 5 种方法!
前些天在实现 MyBatis 批量插入时遇到了一个问题,当批量插入的数据量比较大时,会导致程序执行报错,如下图所示: 原因是 MySQL 只能执行一定长度的 SQL 语句,但当插入的数据量较多时,会生 ...
- 寒武纪加速平台(MLU200系列) 摸鱼指南(一)--- 基本概念及相关介绍
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- 移动GPU分类/百科
ARM mali gpu四大微架构概述 https://zhuanlan.zhihu.com/p/107141045 http://www.neardi.com/news_23/487.html
- 开发中常见的@NotNull,@NotBlank,@NotEmpty注解的区别
开发中常看见@NotNull,@NotBlank,@NotEmpty三个注解,但却没有深入了解过,下面介绍一下他们的应用场景和区别 @NotNull:主要用在基本数据类型上(Int,Integer,D ...
- Spring的轻量级实现
作者: Grey 原文地址:Spring的轻量级实现 本文是参考公众号:码农翻身 的从零开始造Spring 教程的学习笔记 源码 github 开发方法 使用TDD的开发方法,TDD的开发流程是: 写 ...
- 4.ASCII码排序
描述 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符. 输入 第一行输入一个数N,表示有N组测试数据.后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之 ...
- 浏览器调用接口正常,jmeter调不通的可能原因
首先,还是http状态码介绍(网上都能找到这些简介): 1xx 信息,服务器收到请求,需要请求者继续执行操作 2xx 成功,操作被成功接收并处理 3xx 重定向,需要进一步的操作以完成请求 4xx 客 ...