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;
    }
};
Duilib的简单使用(五、DuiDesigner/DuiEditor的下载编译) 一、前言 在Duilib的简单使用(一、duilib demo)中我们介绍了利用duilib简单的构造一个项目 在Duilib的简单使用(二、xml实现界面与业务分离)中我们介绍了XML在duilib中的使用 在Duilib的简单使用(三、界面逻辑交互)中我们已经知道如何简单的进行界面的交互 在Duilib的简单使用(四、换肤)中,我们提了一下Duilib强大的一键换肤功能。 通过之前的说明,我们知道duilib是通过xml来分离界面和业务逻辑的,但是xml如果不能预览,那每次敲一个再运行一下看看效果,这不 阅读详情

相关推荐

CNN经典论文(一)LeNet

Gradient-Based Learning Applied to Document Recognition(梯度学习在文档识别中的应用) Yann LeCun, Leon Bottou, Yoshua Bengio, and Patrick Haffner 发表时间:December 1998 期刊:Proceedings of the IEEE 影响:世界上第一个卷积神经网络 论文地址:Gradient-Based Learning Applied to Document Recognition

loveling丶的博客 3599

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 –>

342

[uboot] (第一章)uboot流程——概述

[uboot] uboot流程系列: [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)建议先看《[project X] tiny210(s5pv210)上电启动流程(BL0-BL2)》,根据例子了解一下上电之后的BL0\BL1\BL2阶段,以及各个阶段的运行位置,功能。==============================================

ooonebook的博客 1万+

[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

vmware vsphere 证书过期告警处理方法

vSphere Clinet 管理页面出现“证书告警” ,检查证书未发现现有证书有过期现象

m0_51902127的博客 3641

primetime如何做voltage scaling

存在这样的情况,标准单元库只有ssgnp0p72vm40c和ssgnp0p9vm40c的libs,但是我们希望它工作在ssgnp0p76vm40c,这样的话就需要用ssgnp0p72vm40c和ssgnp0p9vm40c的libs来scaling出ssgnp0p76vm40c的lib。

qq_34326957的博客 1685

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

实战演练:用wvp-GB28181-pro + ZLMediaKit搭建一个可用的国标视频监控测试环境(附设备模拟器对接)

本文详细介绍了如何使用wvp-GB28181-pro和ZLMediaKit在Ubuntu系统上搭建GB28181视频监控测试环境。从环境准备、组件解析到核心服务部署与设备模拟器对接,提供完整的实战指南,帮助开发者快速构建可验证的监控系统。

weixin_42692935的博客 580

[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

单细胞数据互转实战:从Scanpy的h5ad到Seurat对象的四种高效方法

本文针对单细胞数据分析中Scanpy的h5ad格式与Seurat对象互转的常见难题,详细对比了SeuratDisk、anndataR、schard和sceasy四种高效转换方法。文章从实战出发,剖析了每种方法的原理、操作流程、优缺点及避坑指南,并提供了清晰的选型策略,帮助用户根据数据特点选择最佳方案,实现跨平台数据的无缝衔接。

weixin_29099217的博客 450

windows下使用Nginx搭建Rtmp流媒体服务器,实现视频直播功能

一、环境介绍 操作系统:win10 64位系统 Linux下搭建Nginx搭建Rtmp流媒体服务器方法请看这里:https://blog.csdn.net/xiaolong1126626497/article/details/105378894 二、流媒体服务器介绍 流媒体服务器的主要功能是以流式协议(RTP/RTSP、MMS、RTMP等)将视频文件传输到客户端,供用户在线观看;也可从视频采集、压缩软件接收实时视频流,再以流式协议直播给客户端。 nginx服务器是开源的高性能的http服务器,反向

1万+

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

ultralytics.data.utils——utils.py子模块代码详读

ultralytics.data.utils——utils.py子模块代码详读

Together_CZ的博客 1722

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

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

[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、付费专栏及课程。

余额充值