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中的strip命令简介
转载:https://blog.csdn.net/qq_37858386/article/details/78559490 strip:去除,剥去 一.下面是man strip获得到的信息,简 ...
- 栈的压入、弹出顺序 牛客网 剑指Offer
栈的压入.弹出顺序 牛客网 剑指Offer 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是 ...
- Ubuntu 16.04 菜单栏 换位置 挪到左边 挪到下边
Ubuntu菜单栏的位置可以调 到左侧 或者底部 调整到底部 $ gsettings set com.canonical.Unity.Launcher launcher-position Bottom ...
- pascals-triangle-ii leetcode C++
Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3, Return[1,3 ...
- axios & fetch 异步请求
// 一.创建实例 const request = axios.create({ baseURL: "http://kg.zhaodashen.cn/v2", headers: { ...
- Python里字符串Format时的一个易错“点”
这是一篇很小的笔记,原因是我做学习通的时候见到了这个题: 当时看了一会儿发现没有符合自己想法的答案,然后就脑袋一热选了C,结果当然是错了... 看了一眼这个format的字符串对象,发现有个 {:7. ...
- matlab与python scipy.signal中 freqs freqz 中w,什么时候是角频率,什么时候是真实的工程中使用的采样频率Hz,如何转化
matlab与python scipy.signal中的freqs,freqz频率分析函数,输出的w,有时候是角频率,有时候是真实频率,容易搞混,这里对比一下. 0. 精要总结: 1) freqs: ...
- win10+MX350显卡+CUDA10.2+PyTorch 安装过程记录 深度学习环境配置
https://blog.csdn.net/m0_37867091/article/details/105788637
- 【java+selenium3】Tesseract-OCR识别图片验证码 (十六)
[java+selenium+Tesseract-OCR(图片识别)+AutoIt(windows窗口识别)]完成自动化图片验证码识别! 一.AutoIt(windows窗口识别)参考:https:/ ...
- Centos7+Postfix+Dovecot实现内网邮件收发
1. 前期准备: 主机:CentOS release 7.6.1810 (Core) #安装时选择邮件服务器 IP:192.168.71.108 #示例 本地yum源 #因为是内网,所以建 ...