Leetcode202-Happy Number

手把手教你玩转RISC-V指令集​​​专栏--​​1.2 对比x86/ARM:RISC-V的三大核心优势(开放、灵活、可扩展)​ ​:RISC-V以“开放打破垄断、灵活适配场景、扩展定义未来”的三大优势,正在重塑计算架构格局。其核心价值不仅是技术革新,更是对全球半导体产业链话语权的重新分配。 阅读详情

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;
    }
};
如代码或分析有误,请批评指正,谢谢。

2025_NIPS_Truthful Aggregation of LLMs with an Application to Online Advertising 大型语言模型(LLMs)生成内容的 revenue 变现是在线广告的下一个前沿领域。我们考虑这样一种场景:广告商旨在影响LLM的回复,而平台则寻求最大化广告商价值并确保用户满意度。核心挑战在于,广告商的偏好通常与用户偏好存在冲突,且广告商可能误报其偏好。为解决这一问题,我们提出MOSAIC——一种拍卖机制,它确保真实申报是广告商的占优策略,并使每个广告商的效用与其对社会福利的贡献保持一致。重要的是,该机制无需LLM微调或访问模型权重,且随着计算资源的增加,可证明收敛到最优微调LLM的输出。 阅读详情

相关推荐

陀螺仪及四元数解算

陀螺仪 四元数

zb313982521的博客 2009

【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

计量经济学——事件研究法/事件分析法

计量经济学——事件研究法/事件分析法

weixin_46189461的博客 6763

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

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

落墨画雪的博客 7196

ZYNQ-7035+HMCAD1511高速数据采集系统设计

高速数据采集是电子测量、雷达信号处理与通信基带验证的核心技术基础,其性能瓶颈常集中于采样率、通道同步性与实时处理能力。基于ADC采样原理与SoC异构架构,高带宽、低抖动时钟链路与FPGA+ARM协同处理成为突破关键。ZYNQ-7035凭借275K逻辑单元与千级DSP资源,显著提升LVDS数据流吞吐与片上滤波/触发等实时算法部署能力;HMCAD1511则以1GSPS四通道同步采样和8位分辨率,满足示波器级动态范围需求。该组合广泛应用于嵌入式信号采集平台开发、教学实验系统构建及国产化仪器原型验证。

weixin_28939623的博客 266

layui 监听单元格点击事件,获取当前点击单元格的内容以及 所在的表头内容

特别是后台的一些管理系统中 业务中还是会碰见这样的需求呢 如果你的后台使用layui写的话 在表格中 layui 也给我们提供了监听单元格的事件 但是好像不能直接获取到单元格内容个 和 对应的表头 其实 还是可以解决 换个角度使劲就会发现新的世界 不是吗 event: 'username' 在每个表头中加一个 event 属性 这样就可以监听表头事件 记住个 event 和 当前列的字段最好保持一样 table.render({ e...

业余前端的职业经历 1万+

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的博客 2260

autodl云服务器使用中的conda源报错

通过find path -name filename找到.condarc文件,在autodl服务器中,一般在/root/.condarc。为了利用jypyter打开conda虚拟环境运行,需要下载ipykenel。这个报错是源的问题,在通过源获取包元数据时出现了HTTP 429错误。通过conda换源解决。修改.condarc文件,命令: vim .condarc。但这样还不能解决问题,得把报错中有问题的源注释掉。通过以下命令添加清华镜像源。把报错的链接源注释如下。

Ty_kilig的博客 1594

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

万字长文:YOLOv5+RK3588 NPU多线程加速全攻略——从理论到部署的终极优化

RK3588 NPU多线程加速优化教程摘要 本教程深入讲解了如何利用线程池技术优化RK3588 NPU上的YOLO目标检测模型推理性能。RK3588内置3个NPU核心,理论算力达6TOPS,为多线程优化提供了硬件基础。教程详细解析了线程池的核心组件(任务队列、工作线程、同步机制)和实现架构,包括类设计、初始化流程、工作线程逻辑(任务获取、NPU推理、结果存储三个阶段)以及任务提交/获取机制。通过预分配模型实例、线程绑定、流量控制等优化手段,实现了从单线程28FPS到多线程120FPS的显著性能提升,为边缘A

LXY_LOVE36的博客 1112

解锁RK3588芯片超强网络性能:多队列网卡与IRQ负载均衡实战

摘要:本文针对RK3588芯片的网络性能优化展开研究,重点探讨了多队列网卡配置和IRQ负载均衡技术。通过配置多队列网卡,将网络流量分散到多个队列处理,显著提升了吞吐量(测试显示提升67%)。同时,通过IRQ负载均衡配置使中断请求均匀分配到各CPU核心,CPU利用率降低至35%。文章详细介绍了配置步骤、测试方法,并分析了常见问题及解决方案。这些优化措施有效提升了RK3588在网络录像机、边缘计算等应用场景中的性能表现,为高性能网络应用提供了有力支持。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 508

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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

[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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

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

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的博客 242

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

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的博客 2017

[LeetCode] 202. Happy Number

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

Reyiiiin的深夜一点半 272

(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的博客 740

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

无名山丘,崛起成峰 899
上一篇: 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、付费专栏及课程。

余额充值