202. Happy Number

202. Happy Number [easy] (Python) 题目链接https://leetcode.com/problems/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 positiv 阅读详情
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
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

使用set即可,注意这道题最终输出要么是因为1结束,要么是由于出现环路结束。注意set返回的是pair,所以要判断set的第二个元素。

class Solution {
public:
    bool isHappy(int n) {
        if(n <= 0)
            return 0;
        set<int> s;
        while((s.insert(n)).second){
            int b = 0;
            while(n != 0){
                int a = n % 10;
                b += a * a;
                n /= 10;
            }
            if(b == 1)
                return true;
            else
                n = b;
        }
        return false;
    }
};
MC33PT2000控制详解一:PT2000 Dev Studio工具的使用 MC33PT2000共可控制6个高低端(如控制6个高压喷油器),1个高低端(如控制高压油泵)和DC-DC模块(如升压到65V). 阅读详情

相关推荐

多任务学习中的过拟合陷阱:动态停止训练机制详解与避坑指南

本文深入剖析了多任务学习中因任务过拟合不同步导致的性能下降问题,并详细介绍了动态停止-继续训练机制。该机制通过为每个任务设置独立的训练开关,基于验证集性能动态控制其梯度更新,有效缓解了任务间负迁移,是提升多任务模型整体泛化能力的关键策略。

happy2的博客 1101

202 Happy Number

203 Remove Linked List Elements连接: https://leetcode.com/problems/remove-linked-list-elements/题目描述如下:Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –>

343

STM32G4实战:用CubeMX+Keil五分钟搞定FreeRTOS多任务LED控制(避坑指南)

本文详细介绍了如何在STM32G4系列芯片上使用CubeMX和Keil快速配置FreeRTOS实现多任务LED控制,包括环境准备、时钟配置、任务创建及常见问题解决方案。重点解决了时钟源冲突等高频问题,并提供了优化建议和进阶应用示例,帮助开发者高效完成嵌入式开发任务。

joy55的博客 243

[LeetCode 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 numbe

瓦尔登湖 4161

麒麟V10系统命令实战指南:从入门到精通

本文为麒麟V10操作系统用户提供了一份从入门到精通的命令行实战指南。内容涵盖文件管理、进程监控、网络诊断、文本处理及系统管理等核心场景,旨在帮助用户转变思维,掌握高效解决实际问题的系统命令技巧,从而提升在麒麟V10环境下的运维与工作效率。

sat99的博客 608

Vue3 - 详解实现网站接入钉钉扫码登录功能,钉钉授权生成登录二维码用户用手机扫描二维码登陆,支持弹框式、浏览器单独新开页显示,钉钉扫描二维码登录详细流程及示例代码,最新vue集成钉钉扫码授权登录!

vue3,nuxt3,钉钉,扫码登录,vue3钉钉扫码登陆,钉钉授权第三方WEB网站扫码登录功能,钉钉扫码登录第三方网站,单点登录,二维码不显示,报错,扫了没反应,出错,vue3钉钉扫码登录,内嵌,获取个人信息,pc网页钉钉登录,钉钉扫码登录怎么新开一页,跳页显示二维码,内嵌登录二维码,iframe新页面弹出,弹出一个小窗口显示登录二维码,登录报错,扫码没反应,解决手机点登录报错没反应,官方文档,手机扫描钉钉二维码登录网站,页面弹出一个小窗口而不是打开一个页面,vue3中使用iframe实现另开小窗口扫码登

🏆 前端领域优质创作者 3711

leetcode 202: Happy Number

Happy Number Total Accepted: 3585 Total Submissions: 10859 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting wi

西施豆腐渣 8848

LVGL 拨盘菜单(Dial Menu)

h。

摸鱼专家 210

[leetcode] 202. Happy Number

Description 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 th...

Learning from the mistakes 289

一个严谨的STM32串口DMA发送&接收(1.5Mbps波特率)机制

本文描述串口DMA收/发机制设计,及STM32串口DMA收/发实现。

只要思想不滑坡,想法总比问题多。 5万+

第一章 EB安装

1.DemoWorkspace 是基于EB当前组件配置下的例程,包含了启动文件和中断服务函数以及组件demo。2.Mclsar 下存放的是EB的插件包和静态MCAL的驱动文件。

舊梦的博客 2000

Leetcode 202 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

无名山丘,崛起成峰 900

ISO21434 组织网络安全管理(二)

为了实现网络安全工程,该组织建立并维护网络安全治理和网络安全文化,包括网络安全意识管理、能力管理和持续改进。这涉及到指定针对本文档的目标进行独立审计的特定于组织的规则和过程。 为了支持网络安全工程,该组织实施了网络安全的管理系统,包括管理工具和应用质量管理系统。

qq_42357877的博客 1338

LeetCode 202: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

哈哈的个人专栏 2361

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Leetcode_202_Happy Number

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45396585 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 th

皮斯特劳沃 2370

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode202:Happy Number

LeetCode202:Happy Number 一、题目地址:https://leetcode.com/problems/happy-number/ 二、题目描述:    Write an algorithm to determine if a number is "happy".    A happy number is a number defined by the fo

tinyskyye的专栏 333

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[202] Happy Number

题目描述为判断一个数是不是Happy数,所谓Happy数就是将n拆分成若干个整数后求平方和,之后重复这个过程,如果最后得到结果1,则结束循环则n是happy数,如果循环一直不结束则n不是happy数。

camlot的博客 359

leetcode 202 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 o

shiter编写程序的艺术 3944

(LeetCode 202) Happy Number

Q: 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

SuperNova 443

happy number java_202. Happy Number

202. Happy NumberWrite 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 o...

weixin_39517902的博客 233

[LeetCode202] 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

coder 进阶的专栏 1934

LeetCode 202. Happy Number

LeetCode 202. Happy Number (Easy) 主要知识点:数学技巧、set的使用、快慢指针;优先级:3.5;数学证明没看

温迪今天刷题了吗 296

leetcode 202 Happy Number C++

这个题我实验的两次发现了一个规律。。。只要结果有4就不可能变成1,别的都可以。 class Solution { public: bool isHappy(int n) { while(1){ n = getSum(n); if(n==1) return true; if(n==4) return f

wcxdell的专栏 1261
上一篇: 67. Add Binary
下一篇: 204. Count Primes(埃拉托色尼)
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址