【easy】202. Happy Number
happy number
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
//写的不对……没有想到用set……问题在于不是1的循环,那样的话就不是一个循环数1,而是一个循环节,要判断之前的数有没有出现
class Solution {
public:
bool isHappy(int n) {
if (n < )
return false;
if (n == )
return true;
unordered_set<int> showedNums;
showedNums.insert(n); while (true) {
int s = ;
while (n) {
s += (n % ) * (n % );
n = n / ;
} if (s == )
return true;
else if (showedNums.find(s) != showedNums.end())
return false;
n = s;
showedNums.insert(s);
}
}
};
【easy】202. Happy Number的更多相关文章
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】202 - Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【easy】268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【easy】263. Ugly Number 判断丑数
class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...
- 181. Flip Bits【easy】
181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- 记一次Maven编译IKAnalyzer失败及解决办法
下载了一个开源项目,maven形式组织的,其中有一个依赖包是IKAnalyzer. 由于mvnrepository中不存在IKAnalyzer的坐标,因此该依赖包需要自己下载安装到本地maven仓库才 ...
- TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ HTTP/1.1" 500 114103 Performing system checks...
Error Msg TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ ...
- 关于表单元素的name及HTML中的id
这种在上高级WEB课时,老师为表单元素赋了name值,之后直接在JS中使用该值而不需要使用document.get...来获取了,例: <!DOCTYPE html> <html&g ...
- Word Representations 词向量
常用的词向量方法word2vec. 一.Word2vec 1.参考资料: 1.1) 总览 https://zhuanlan.zhihu.com/p/26306795 1.2) 基础篇: 深度学习wo ...
- red()、redinle()、redlines()三者之间的关系
# 关于read()方法: # 1.读取整个文件,将文件内容放到一个字符串变量中 # 2.如果文件大于可用内存,不可能使用这种处理 file_object = open("a.txt&quo ...
- 并发容器学习—ConcurrentSkipListMap与ConcurrentSkipListSet 原
一.ConcurrentSkipListMap并发容器 1.ConcurrentSkipListMap的底层数据结构 要学习ConcurrentSkipListMap,首先要知道什么是跳表或跳 ...
- Elasticsearch6.5.2 X-pack破解及安装教程
先正常安装 elasticSearch, kibana. 1. 如果是6.5.2版本,可以直接下载jar文件:https://download.csdn.net/download/bigben0123 ...
- Springboot 3.需求携带参数的get请求
还是拿来上节讲的代码: package com.course.server; import org.springframework.web.bind.annotation.*; import java ...
- Vue(一)安装
环境准备 这里我们就直接使用官方推荐的Vue CLI方式 CLI (@vue/cli) 是一个全局安装的 npm 包,提供了终端里的 vue 命令.它可以通过 vue create 快速创建一个新项目 ...
- MongoDB 高可用集群副本集+分片搭建
MongoDB 高可用集群搭建 一.架构概况 192.168.150.129192.168.150.130192.168.150.131 参考文档:https://www.cnblogs.com/va ...