Leetcode202-Happy Number

IC验证 AHB-RAM 项目(二)——接口与事务代码的编写 本文介绍了准备工作及接口、事务相关代码编写要点。在接口部分,重点说明了lvc_ahb_if.sv、rkv_ahbram_if.sv和rkv_ahbram_tb.sv三个关键文件的功能,其中rkv_ahbram_if.sv通过动态任务实现复位信号控制。事务部分则涉及lvc_ahb_types.sv中的枚举类型定义和lvc_ahb_transaction.sv中的随机化变量设置,用于构建AHB传输激励。整个验证环境采用模块化设计,通过接口实现TB与DUT的通信,为后续验证工作奠定基础。 阅读详情

Leetcode202-Happy Number(Easy)

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.


题意:

happy数的定义如下:给定一个正整数,用其各位数的平方和取代该数,重复此操作直到该数等于1,或形成一个不为1的循环。其中,前者成为happy数。

例子:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1


分析:

题目中已经给了提示,当能出现1时,即退出循环返回true;如出现了一个不为1的循环则返回false(如4, 16, 37, 58, 89, 145, 42, 20, 4, ...)。对happy number的详细描述见wiki百科:Happy Number。wiki还对Happy primes和Harshad number做了一些解释。为了快速确定是否发生非1的重复,使用unordered_set来记录已经出现过的数(我们并不关心数字的顺序,因此用hash效率更高)。


AC代码:

class Solution {
public:
    bool isHappy(int n) {
        int sum;
        unordered_set<int> check;
        check.insert(n);
        while (n != 1) {
        	sum = 0;
        	while (n) {
        		int t = n % 10;
        		n /= 10;
        		sum += t * t;
        	}
        	if (check.find(sum) != check.end())
        		return false;
        	check.insert(sum);
        	n = sum;
        }
        return true;
    }
};
如代码或分析有误,请批评指正,谢谢。

LVGL实战 透视卡盘 八张卡立在一个圆柱面上,摄像机在柱心前方。每帧对屏幕上的每个像素发一条射线,与卡片所在平面求交,得到三维交点,再投影回卡片自己的二维坐标系取样。透视、倾斜、近大远小、相互遮挡 —— 全部是这一步的副产物。 阅读详情

相关推荐

CANopen中SDO、PDO字典以及COB-ID理解

CANopen SDO PDO

sunarmy的专栏 2万+

【python】opencv实现椭圆拟合与绘制,不能用float型数据进行运算!

opencv版本: 版本这么高,没想到做个椭圆拟合与绘制还有问题。 opencv的椭圆拟合函数有三个: cv2.fitEllipse() cv2.fitEllipseAMS() cv2.fitEllipseDirect() 函数细节见官方文档 椭圆绘制函数:cv2.ellipse() 详见参考文章 著名难题: python opencv ellipse takes at most 5 arguments 网上最为流行的解决方案:改int,改int就完事了,还有人因此断定这个..

weixin_44340194的博客 2041

wimdows11 eplan安装过程及遇到的问题

eplan 安装 我的资源里的下载地址,当然也可以有其他渠道下载 https://download.youkuaiyun.com/download/weixin_45529296/85061161 安装1 打开“Electric P8 2.7.3.11418”目录,运行“setup.exe”开始安装,选择系统和程序 接受许可协议,下一步 自己配置安装的各种路径,下一步 安装这个没什么问题,直到完成就行; 安装2 ...

csdn-1122 2万+

python:TypeError:takes at least 8 arguments (9 given)

1:背景 今天遇见一个问题,如标题所示,在python项目中,出现了TypeError:takes at least 8 arguments (9 given)的错误。首先,毋庸置疑的是,形参和实参不是对应的,参数传错导致。秉持着严谨的态度,想简单统计下因为参数传错导致的不同错误类型。 2:分析过程 1首先直接先解决标题问题(可能有py2和py3混用,不影响结果) 先看如下过程,出现了标题类似错误,原因在传参不匹配,被调用函数需要至少3个位置参数就可以正常运行,不需要传递关键字参数,因为调用的语句少传递一个

落墨画雪的博客 7190

超详细neo4j安装教程

一、neo4j简介 最近开始学习知识图谱,所以首先想先学习一下neo4j的使用。 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。由于知识图谱中存在大量的关系型信息(实体—关系—实体), 使用结构化数据库进行存储将产生大量的冗余存储信息, 因此将图数据库作为知识图谱的存储容器成为流行的选择。当前较为常用的图数据库主要有 Neo4j 等。 二、neo4j的安装 功能快捷键 撤销:Ctrl/Command + Z 重做:Ctrl/Command + Y 加粗:Ctrl/Co

qq_38335648的博客 9万+

华为[ENSP]OSPF的配置实例(单区域+多区域)

华为[ENSP]OSPF的配置实例(单区域+多区域)

weixin_62594100的博客 2万+

TypeError: zeros() takes at most 3 arguments (4 given)

在执行如下程序时: reshape_frames = np.zeros(video_all_frames.shape[0],224,224,video_all_frames.shape[3]) 发生如下错误: TypeError: zeros() takes at most 3 arguments (4 given) 原因分析: zeros函数定义为() zeros(shape, dtype=float, order='C') 一重括号会使系统认为依次给 hape, dtype=float, o

qq_35568823的博客 2259

Claude Code × SolidWorks:批量填属性、格式转换、BOM 汇总自动化实战

摘要:本文介绍利用 Claude Code 生成 VBA 宏实现 SolidWorks 批量处理任务的自动化方案。包含5个实战案例:批量填写自定义属性(60分钟任务→10秒)、批量格式转换(80分钟→30秒)、多配置工程图自动生成(120分钟→2分钟)、滚珠丝杠选型计算(15分钟→10秒)和装配体递归BOM导出(40分钟→1分钟)。所有代码基于SolidWorks 2020+ API,提供完整VBA实现和操作指南,可显著提升设计效率。

m0_64424835的博客 612

leetcode刷题:Happy Number

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 equa

a1204800782的专栏 575

【Tessy】集成测试小白入门指导

本文详细介绍了如何进行集成测试的小白入门指导,从新建工程到设计用例,实现集成测试的过程详细步骤

qq_37282365的博客 1185

医院批量排班实战指南(三)日期列表生成与时段批量操作

在上一篇《批量排班功能实现指南(二):日期与时段设置》中,我们完成了日期范围选择和重复规则配置的界面搭建,实现了从第一步到第二步的跳转功能。现在我们已经可以选择日期范围和重复规则,但还缺少最关键的功能——根据规则生成日期列表并进行时段配置。本篇将继续深入批量排班的核心功能——日期列表生成与时段批量操作。根据日期范围和重复规则智能生成日期列表为每个日期配置上午、下午、夜诊时段批量时段操作提升配置效率日期列表的展示与管理。

低代码布道师的博客 1346

LeetCode:Happy Number

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

flowbird的博客 419

从0到全栈:C#/.NET/.NET Core终极学习路线——2025年开发者必修的12个阶段与30个实战项目

【代码】从0到全栈:C#/.NET/.NET Core终极学习路线——2024年开发者必修的12个阶段与30个实战项目。

墨夶的博客 2042

[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 288

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

[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

瓦尔登湖 4160

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

哈哈的个人专栏 2360

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Leetcode_202_Happy Number

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.youkuaiyun.com/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

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

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 s...

zhangjun62的博客 240

[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

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

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编写程序的艺术 3936

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

Yano_nankai的博客 2016

[LeetCode] 202. Happy Number

题目内容 https://leetcode-cn.com/problems/happy-number/ 编写一个算法来判断一个数是不是“快乐数”。 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。 示例: 输入: 19 输出: tru...

Reyiiiin的深夜一点半 271

(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

Leetcode 202. Happy Number 快乐数

Leetcode 202. Happy Number 快乐数202. Happy Number 快乐数题目描述示例:解答1代码1解答2代码2 202. Happy Number 快乐数 题目描述 Share Write an algorithm to determine if a number is “happy”. A happy number is a number defined by th...

importsys的博客 738
上一篇: Leetcode201-Bitwise AND of Numbers Range
下一篇: Leetcode204-Count Primes
coderhuhy
博客等级 码龄12年 24粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值