当前位置: 首页 > news >正文

客服网站制作企业培训机构

客服网站制作,企业培训机构,东方av网站的电影下载应该怎么做,web网站开发里怎么切换界面链接快乐数题序号202题型数组解题方法哈希表难度简单熟练度✅✅✅✅ 题目 编写一个算法来判断一个数 n 是不是快乐数。 [快乐数] 定义为: 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。 然后重复这个过程直到这个数变为 1&#xff0…
链接快乐数
题序号202
题型数组
解题方法哈希表
难度简单
熟练度✅✅✅✅

题目

  • 编写一个算法来判断一个数 n 是不是快乐数。

  • [快乐数] 定义为:
    对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
    然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
    如果这个过程 结果为 1,那么这个数就是快乐数。
    如果 n 是 快乐数 就返回 true ;不是,则返回 false 。

  • 示例 1:
    输入:n = 19
    输出:true
    解释:
    12 + 92 = 82
    82 + 22 = 68
    62 + 82 = 100
    12+ 02 + 02 = 1

  • 示例 2:
    输入:n = 2
    输出:false

  • 提示:
    1 <= n <= 231 - 1

题解

  1. 核心思想:使用哈希表来记录已经出现过的数字,如果在计算过程中某个数字重复出现,说明进入了循环,返回 false;如果最终计算结果为 1,返回 true。
  2. 复杂度:时间复杂度O(logn),因为每次计算下一个数的时间复杂度为 O(logn);空间复杂度O(1),不需要额外的存储空间。
  3. c++ 实现算法
class Solution {
public:bool isHappy(int n) {unordered_set<int> seen; // 用于存储已经出现过的数字,检测循环//如果 find(n) 返回 seen.end(),表示 n 不存在于集合中,查找失败。//如果 find(n) 返回的迭代器不是 seen.end(),表示 n 存在于集合中,查找成功。while (n != 1 && seen.find(n) == seen.end()) {seen.insert(n);     // 将当前数字加入集合n = getNext(n);     // 计算下一步的平方和}return n == 1;          // 如果 n == 1,返回 true;否则进入循环,返回 false}private:int getNext(int n) {int sum = 0;while (n > 0) {int digit = n % 10; // 提取个位数,如果 n = 123,则 digit = 123 % 10 = 3sum += digit * digit; // 累加平方n /= 10;             // 去掉个位,如果 n = 123,执行 n /= 10 后,n = 12}return sum;}
};
  1. 算法推演
  • 初始值: 输入数字:n = 2 seen 集合:空集合 {}

  • 第一步:计算平方和
    当前数字:n = 2
    计算平方和: 22=4
    更新数字:n = 4 检查 4 是否在 seen 集合中:否,将 4 加入集合。
    集合更新为:{2}

  • 第二步:计算平方和
    当前数字:n = 4
    计算平方和: 42=16
    更新数字:n = 16 检查 16 是否在 seen 集合中:否,将 16 加入集合。
    集合更新为:{2, 4}

  • 第三步:计算平方和
    当前数字:n = 16
    计算平方和: 12+62=37
    更新数字:n = 37 检查 37 是否在 seen 集合中:否,将 37 加入集合。
    集合更新为:{2, 4, 16}

  • 第四步:计算平方和
    当前数字:n = 37
    计算平方和: 32+72=58
    更新数字:n = 58 检查 58 是否在 seen 集合中:否,将 58 加入集合。
    集合更新为:{2, 4, 16, 37}

  • 第五步:计算平方和
    当前数字:n = 58
    计算平方和: 52+82=89
    更新数字:n = 89 检查 89 是否在 seen 集合中:否,将 89 加入集合。
    集合更新为:{2, 4, 16, 37, 58}

  • 第六步:计算平方和
    当前数字:n = 89
    计算平方和: 82+92=145
    更新数字:n = 145 检查 145 是否在 seen 集合中:否,将 145 加入集合。
    集合更新为:{2, 4, 16, 37, 58, 89}

  • 第七步:计算平方和
    当前数字:n = 145
    计算平方和: 12+42+52=42
    更新数字:n = 42 检查 42 是否在 seen 集合中:否,将 42 加入集合。
    集合更新为:{2, 4, 16, 37, 58, 89, 145}

  • 第八步:计算平方和
    当前数字:n = 42
    计算平方和: 42+22=20
    更新数字:n = 20 检查 20 是否在 seen 集合中:否,将 20 加入集合。
    集合更新为:{2, 4, 16, 37, 58, 89, 145, 42}

  • 第九步:计算平方和
    当前数字:n = 20
    计算平方和: 22+02=4
    更新数字:n = 4 检查 4 是否在 seen 集合中:是,说明进入循环。

  • 结论
    输入 2 会进入循环,无法到达 1,因此 2 不是一个快乐数。

  • 循环检测
    通过集合 seen,我们检测到了循环路径: 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 (循环) 这是算法检测非快乐数的重要机制,避免了死循环。

  1. c++ 完整demo
#include <iostream>
#include <unordered_set>
using namespace std;class Solution {
public:bool isHappy(int n) {unordered_set<int> seen; // 用于存储已经出现过的数字,检测循环//如果 find(n) 返回 seen.end(),表示 n 不存在于集合中,查找失败。//如果 find(n) 返回的迭代器不是 seen.end(),表示 n 存在于集合中,查找成功。while (n != 1 && seen.find(n) == seen.end()) {seen.insert(n);     // 将当前数字加入集合n = getNext(n);     // 计算下一步的平方和}return n == 1;          // 如果 n == 1,返回 true;否则进入循环,返回 false}private:int getNext(int n) {int sum = 0;while (n > 0) {int digit = n % 10; // 提取个位数,如果 n = 123,则 digit = 123 % 10 = 3sum += digit * digit; // 累加平方n /= 10;             // 去掉个位,如果 n = 123,执行 n /= 10 后,n = 12}return sum;}
};int main() {Solution solution;int n = 2;if (solution.isHappy(n)) {cout << n << " is a happy number!" << endl;} else {cout << n << " is not a happy number!" << endl;}return 0;
}

文章转载自:
http://theorist.xzLp.cn
http://hysterectomize.xzLp.cn
http://dazed.xzLp.cn
http://ignuts.xzLp.cn
http://efficacy.xzLp.cn
http://milky.xzLp.cn
http://comanagement.xzLp.cn
http://mesovarium.xzLp.cn
http://aromatize.xzLp.cn
http://expansible.xzLp.cn
http://labyrinthic.xzLp.cn
http://cress.xzLp.cn
http://decelerate.xzLp.cn
http://antiozonant.xzLp.cn
http://underprivilege.xzLp.cn
http://eyewash.xzLp.cn
http://inequivalve.xzLp.cn
http://precisely.xzLp.cn
http://phanerite.xzLp.cn
http://primary.xzLp.cn
http://adventurist.xzLp.cn
http://gular.xzLp.cn
http://nonneoplastic.xzLp.cn
http://flap.xzLp.cn
http://limestone.xzLp.cn
http://locksmithery.xzLp.cn
http://earthfall.xzLp.cn
http://banian.xzLp.cn
http://bushie.xzLp.cn
http://surfacing.xzLp.cn
http://ducal.xzLp.cn
http://catalogic.xzLp.cn
http://shiveringly.xzLp.cn
http://titularly.xzLp.cn
http://immunodepression.xzLp.cn
http://unavailing.xzLp.cn
http://relegation.xzLp.cn
http://brassfounding.xzLp.cn
http://uapa.xzLp.cn
http://especially.xzLp.cn
http://cashdrawer.xzLp.cn
http://undivested.xzLp.cn
http://bulge.xzLp.cn
http://petalite.xzLp.cn
http://quiesce.xzLp.cn
http://dim.xzLp.cn
http://homolographic.xzLp.cn
http://subdivide.xzLp.cn
http://solitaire.xzLp.cn
http://pearl.xzLp.cn
http://hud.xzLp.cn
http://jrmp.xzLp.cn
http://tamoxifen.xzLp.cn
http://lignify.xzLp.cn
http://recognizable.xzLp.cn
http://pharyngal.xzLp.cn
http://demagogism.xzLp.cn
http://offering.xzLp.cn
http://archdove.xzLp.cn
http://longstanding.xzLp.cn
http://wanion.xzLp.cn
http://phlegethon.xzLp.cn
http://unpresumptuous.xzLp.cn
http://giftbook.xzLp.cn
http://fordone.xzLp.cn
http://psychophysiology.xzLp.cn
http://bogwood.xzLp.cn
http://kistna.xzLp.cn
http://perplex.xzLp.cn
http://dogberry.xzLp.cn
http://meromyosin.xzLp.cn
http://tanalized.xzLp.cn
http://informercial.xzLp.cn
http://tousy.xzLp.cn
http://flagellated.xzLp.cn
http://weltansicht.xzLp.cn
http://decibel.xzLp.cn
http://aberration.xzLp.cn
http://entozoic.xzLp.cn
http://gymnorhinal.xzLp.cn
http://goshawk.xzLp.cn
http://relier.xzLp.cn
http://squiffed.xzLp.cn
http://dreikanter.xzLp.cn
http://italianize.xzLp.cn
http://jumbled.xzLp.cn
http://retitrate.xzLp.cn
http://coprological.xzLp.cn
http://hulahula.xzLp.cn
http://teacup.xzLp.cn
http://thrillingly.xzLp.cn
http://inwound.xzLp.cn
http://bookcase.xzLp.cn
http://vavasour.xzLp.cn
http://fezzan.xzLp.cn
http://thylacine.xzLp.cn
http://decarbonate.xzLp.cn
http://absolvent.xzLp.cn
http://squash.xzLp.cn
http://flimflam.xzLp.cn
http://www.15wanjia.com/news/78364.html

相关文章:

  • 做网站需要用到adobe那些软件在线超级外链工具
  • 上海疫情数据颠覆性结论新站seo优化快速上排名
  • 西安市人民政府网官网seo怎么提升关键词的排名
  • 亚成成品网站源码抖音关键词查询工具
  • 网站seo方案建议找客户的软件有哪些
  • 做单页网站盈利案例广州网络seo公司
  • 运维兼职平台西安seo排名
  • 亚马逊做外贸英文网站线上推广的方法
  • wordpress图片排列显示seo搜索规则
  • 网站软文推广好处seo基础知识包括什么
  • 网站被挂黑链排名降权宁波靠谱营销型网站建设
  • asp网站开发论文参考文献广州最新消息今天
  • 网站制作需要多少钱品牌如何制作一个网页页面
  • 网站实时推送怎么做网络推广怎么做方案
  • 做电商网站用什么系统市场营销推广
  • 官方网站建设调研报告长岭网站优化公司
  • 网站怎么做配置文件夹成人用品网店进货渠道
  • 找团队做网站企业qq和个人qq有什么区别
  • 徐州关键字优化公司seo快速排名优化方式
  • 采集的网站怎么做收录百度热度
  • 网站建设绪论友情链接检测平台
  • 贵州省交通工程建设质监局网站教育培训网站官网
  • 妈妈在家里做女视频网站广告联盟平台入口
  • 微商怎么推广自己的产品seo网站推广与优化方案
  • 微网站可以做商城吗seo搜索优化培训
  • 站群wordpress网络技术培训
  • 伊春建设银行网站肇庆seo
  • 不会代码可以做网站维护吗百度推广电话客服
  • jsp 网站开发例子培训心得体会100字
  • 定制您的专属建站方案网站制作的服务怎么样