Leetcode 202:Happy Number

从零开始的BW(五) 外部系统供数之OData测试 5、激活添加的服务,如提示下图中的激活失败,根据生成URI在事务代码SICF目录中找到对应的服务,设置超时时间,设置完之后重新激活即可,详情可参考SAP note 1986626。如果刚才设置BY Odata的query在这没有显示,SE38执行程序EQ_RS_AUTOSETUP,选中需要生成服务的query,生成服务后再去添加服务。测试get方法,URI中默认的参数是?4、事务代码/IWFND/MAINT_SERVICE中添加服务,如果提示用户不再登陆,在事务代码前加上/O或者/N即可。 阅读详情

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
  • 【算法思路】:根据Tag提示,可能要用到哈希表和数学常用库。哈希表用来查重,看计算的时候是否存在循环:若出现循环,直z接判断是非快乐数,返回false;没有的话,放在哈希表,用来后面的查重。while(ln!=0) { sum += Math.pow(n%10, 2); n /= 10;} 计算一个数的平方,Math.pow(a,b),a的b次方,比 n = a*a+b*b+c*c更优很多,不受几位数的限制,使用起来方便实用。</p><p></p><p>java代码:</p><pre name="code" class="java">
  • public class Solution {public boolean isHappy(int n) {
            if(n<=0)
                return false;  
            long ln = n;  
           // 同计算循环小数一样, 如果出现循环, 则无需继续计算,直接返回false即可. 
            Set<Long> set = new HashSet<Long>();  
              
            while(ln<=Integer.MAX_VALUE) {  
                if(set.contains(ln) ) 
                    return false; //有循环,直接判断是非快乐数
                else set.add(ln);  //没有,则添加到哈希表里来,用来后面的查重
                ln = digitSquare(ln);  
                if(ln == 1) 
                    return true;  
            }  
            return false;  
        }  
          
        private long digitSquare(long n) { 
            long sum = 0;  
            while(n!=0) {  
                sum += Math.pow(n%10, 2);  //计算一个数的平方,比 n = a*a+b*b+c*c更优很多
                n /= 10;  
            }  
            return sum;  
        }  
    }

    Testcase:

    n=1是快乐数,n = 2-6不是快乐数;

    当n = 2,2*2=4,4*4=16,1*1+6*6=37,3*3+7*7=58,5*5+8*8=89,8*8+9*9=145,1*1+4*4+5*5=42,4*4+2*2=20,2*2+0=4;出现了循环;

    当n = 3,3*3= 9,9*9=81,8*8+1*1=65,6*6+5*5=61,6*6+1*1=37,3*3+7*7=58,5*5+8*8=89,8*8+9*9=145,1*1+4*4+5*5=42——20——4,会进入n=2时的循环当中

    ……

    当n=6,6*6= 36,3*3+6*6=45,4*4+5*5=41,4*4+1*1=17,1*1+7*7=50——25——29——85——89——145——42——20——4,又进入到n=2的循环中;

    当n=7,7*7=49,4*4+9*9=97——130——10——1

    当n= 8,8*8=64——52——29——85——89——145——42——20——4,又进入到n=2的循环中;

    当n =9,9*9=81——65——61——37——58——89——145——42——20——4,

    综上所述,归纳得n = 2-6不是快乐数;所有的非快乐数,非快乐数都是通过最后n=4,在2~6之间进入循环的。


    一种更比较巧妙地解法,利用 数学归纳法原理。由于非快乐数都是通过n=4进入循环,所以代码也可以:

    public class Solution {
        public boolean isHappy(int n) {
           
            if(n == 1)
              return true;
              
            else if(n>=2&&n<=6)
              return false;
              
             while(n>6){  
                int next = 0;  
                while(n!=0){
                  next+=(n%10)*(n%10); 
                  n/=10;
                }  
                n = next;  
            }  
            return n==1;  
        }  
    }






保姆级教学,教你在vscode和cursor上调试matlab!!! 保姆级教你如何在vscode或者cursor上面调试matlab 阅读详情

相关推荐

避坑指南:用pygrib处理ERA5气象数据时遇到的5个典型问题

本文总结了使用pygrib库处理ERA5气象GRIB数据时常见的五个典型问题及解决方案,涵盖数据获取、内存管理、时间处理、空间插值和性能优化。重点针对气象数据插值这一核心挑战,提供了处理不规则网格、缺失值和跨日期变更线的实用代码示例与优化策略,帮助用户高效、稳定地完成气象数据分析工作。

weixin_28224217的博客 187

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

传输线的特征阻抗

信号源内阻与所接传输线的特性阻抗大小相等且相位相同,或传输线的特性阻抗与所接负载阻抗的大小相等且相位相同,分别称为传输线的输入端或输出端处于阻抗匹配状态,简称为阻抗匹配。否则,便称为阻抗失配。有时也直接叫做匹配或失配。指一个电路输入端的等效阻抗。在输入端上加上一个电压源U,测量输入端的电流I,则输入阻抗Rin就是U/I。可以把输入端想象成一个电阻的两端,这个电阻的阻值,就是输入阻抗。- 对于电压驱动的电路,输入阻抗越大,则对电压源的负载就越轻,因而就越容易驱动,也不会对信号源有影响;

学海无涯的专栏 4475

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

UReport2 报表设计器 在线表格

UReport2官网 一、UReport2报表设计器 UReport2是一个开源的可视化报表设计器,功能强大,操作简单,可以实现复杂的报表统计,有各种各样的导出和打印功能,支持导入Excel表格! 1、报表设计器 2、设计一个简单的用户列表展示 二、可编辑表格 扩展了layui的表格,支持多元化数据编辑(单选,多些、日期、下拉联动),开发简单、高效、便捷。有了它就可以轻松开发类似进销存系统的表格录入功能了,用户拿来即用! 三、主从表录......

ejinxian的专栏 2万+

STM32+A4988控制步进电机

基于A4988芯片驱动42步进电机,在STM32F1上进行测试,最高可以实现16细分。

个人博客 4万+

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

保姆级教程:用GPU-Burn给你的NVIDIA Tesla服务器做个“压力体检”(附温度功耗监控脚本)

本文提供了一份详细的GPU服务器压力测试指南,重点介绍如何使用GPU-Burn工具对NVIDIA Tesla服务器进行全面的性能测试和温度功耗监控。从环境部署、多GPU测试策略到专业级监控指标解析,帮助运维人员构建标准化的GPU健康评估体系,确保AI训练和科学计算等关键业务的稳定性。

weixin_30619101的博客 399

leetcode: 202. Happy Number

leetcode: 202. Happy Number 原题链接 class Solution { public: bool isHappy(int n) { unordered_map<int, bool> mp; while (n != 1) { mp[n] = true; int m = 0; int tmp = n; while (tmp != 0) {

LSK的博客 3199

ROS与深度相机入门教程:(1)Ubuntu16.04 在ROS中驱动Intel D435i深度相机

Intel在Github上开源了支持所有RealSense系列相机的SDK,包括了D400、SR300系列深度相机和T265双目跟踪相机,支持Linux、Windows、Mac OS以及Android,链接:https://github.com/IntelRealSense/librealsense/blob/master/doc/distribution_linux.md#installin...

m0_37957160的博客 2083

手把手教您同花顺如何设置大盘分时均价线

本文将手把手教你如何在同花顺PC端开启大盘指数分时图📈

XiaoqiangClub的博客 4615

[leetcode]: 202. Happy Number

1.题目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 square

沐沐余风的专栏 592

STM32实现简单的智能电梯控制

上述代码实现了一个简单的智能电梯控制器。电梯的状态包括IDLE(停止)、UP(向上移动)和DOWN(向下移动)三种状态。通过向上和向下按钮的按下,可以控制电梯移动到指定的楼层。在主循环中,不断读取当前楼层的按钮状态,并根据电梯状态决定执行的动作。具体的楼层数量和按钮的GPIO引脚可以根据实际情况进行调整。要实现简单的智能电梯控制,我们可以使用STM32微控制器来实现。

2401_85258012的博客 339

LeetCode 202: 快乐数 Happy Number

题目: 编写一个算法来判断一个数是不是 “快乐数”。一个 “快乐数” 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为...

爱写Bug 379

温度PID控制PLC程序

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

STM32F407中文手册

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

LeetCode笔记:202. Happy Number

判断一个数happy不happy。。。什么鬼

Minecraft 1488

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

leetcode[202]:Happy Number

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 of the

hahubaba的博客 291

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Leetcode 202: Happy Number

问题描述: Write an algorithm to determine if a number n 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. Repeat the process until the number e

weixin_51401145的博客 235

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

sinat_35425429的博客 192

Leetcode 202题:Happy Number的2种解法

题目描述 确定某个数n是否是happy number。关于happy number的定义如下: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does

weixin_44607838的博客 657

leetcode:202. Happy Number

又是放弃治疗的题,,, 肯定有别的简单的方法,菜,,,已经懒得思考了 class Solution { public: bool isHappy(int n) { if(n==1) return true; int sum=0; int a=n%10; int b=n/10; sum=pow(a,2)+p...

hunter 248

LeetCode 202 : Happy Number

如果直接写会发现一个问题,当数字不是happy number 时会陷入死循环。有个trick ,如果有重复就不是happy number , 所以需要用一个哈希表去存算过之后的数,如果重复跳出循环。 这里可以用到set容器去存,find()能找出这个数的index,如果不在set中返回end()。 class Solution { public: bool isHappy(int n)

shensccs的专栏 335

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 of i

Genius 348

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

tuobadon的专栏 474

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

卷卷萌的博客 193
下一篇: Java 字符串和整型的转换
Smith_David
博客等级 码龄11年 1粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值