Leecode刷题之旅-C语言/python-202快乐数
/*
* @lc app=leetcode.cn id=202 lang=c
*
* [202] 快乐数
*
* https://leetcode-cn.com/problems/happy-number/description/
*
* algorithms
* Easy (52.26%)
* Total Accepted: 12.9K
* Total Submissions: 24.7K
* Testcase Example: '19'
*
* 编写一个算法来判断一个数是不是“快乐数”。
*
* 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到
* 1。如果可以变为 1,那么这个数就是快乐数。
*
* 示例:
*
* 输入: 19
* 输出: true
* 解释:
* 1^2 + 9^2 = 82
* 8^2 + 2^2 = 68
* 6^2 + 8^2 = 100
* 1^2 + 0^2 + 0^2 = 1
*
*
*/ int Num(int x)
{
int ret=;
while(x){
ret+=(x%)*(x%);
x/=;
}
return ret;
}
bool isHappy(int n) {
if(n<=)
return false;
while(n!=){
n=Num(n);
if(n==)
return false;
}
return true;
}
这里写了一个函数,专门用来累和的。
然后这里有个小知识,如果无限循环的话,最后结果永远都是4.
-----------------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=202 lang=python3
#
# [202] 快乐数
#
# https://leetcode-cn.com/problems/happy-number/description/
#
# algorithms
# Easy (52.26%)
# Total Accepted: 12.9K
# Total Submissions: 24.7K
# Testcase Example: '19'
#
# 编写一个算法来判断一个数是不是“快乐数”。
#
# 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到
# 1。如果可以变为 1,那么这个数就是快乐数。
#
# 示例:
#
# 输入: 19
# 输出: true
# 解释:
# 1^2 + 9^2 = 82
# 8^2 + 2^2 = 68
# 6^2 + 8^2 = 100
# 1^2 + 0^2 + 0^2 = 1
#
#
#
class Solution(object):
def isHappy(self, n):
# Write your code here
if n is None:
return False
tmp = 0
while tmp != 1 and tmp != 4:
tmp = 0
n = str(n)
for i in n:
tmp += int(i) ** 2
if tmp == 1:
return True
n = tmp
if tmp == 1:
return True
return False
Leecode刷题之旅-C语言/python-202快乐数的更多相关文章
- Leecode刷题之旅-C语言/python-9.回文数
/* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
随机推荐
- 程序单一实例实现 z
不少应用程序有单一实例的需求,也就是同时只能开启一个实例(一般也就是一个进程). 实现的方式可能有判断进程名字,使用特殊文件等等,但是最靠谱的方式还是使用系统提供的 Mutex 工具. Mutex是互 ...
- Docker_1 安装Docker-CE
安装 免sudo运行docker命令 ustc mirrors service failed 安装 Docker-CE 安装过程参考官网,Ubuntu中如下: ## 1. 从仓库安装 $ sudo a ...
- Linux配置临时IP和网关命令
配置IP以及子网掩码: ifconfig eth0 192.168.1.33 netmask 255.255.255.0 up 设置网关: route add default gw 192.168 ...
- 看了xici有写给孩子的信,maybe我也要写给孩子一些东西了
看了xici有写给孩子的信,maybe我也要写给孩子一些东西了
- SQLmap简介以及防火墙绕过方法
简介 许多现实中对于网站的攻击往往是由于网站没有及时更新或者对于用户的输入没有进行检查.从缓冲区溢出说起,这样一种针对系统脆弱性的威胁,最根本的问题还是在于对于用户的输入没有进行检查.作为主要威胁之一 ...
- define常量
看手册说define定义的常量只允许: 仅允许标量和 null.标量的类型是 integer, float,string 或者 boolean. 也能够定义常量值的类型为 resource ,但并不推 ...
- Makefile 实例
CROSS_COMPILE = HI_CFLAGS= -Wall -O2 -g -march=armv7-a -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=vfpv ...
- 通过渲染器Shader实现图像变换效果
在上一篇文章中,一起学习了通过设定画笔风格来实现图形变换,没读过的朋友可以点击下面链接: http://www.cnblogs.com/fuly550871915/p/4886455.html 是不是 ...
- Cacti监控mysql数据库server实现过程
前言:cactiserver端安装请參考:http://blog.csdn.net/mchdba/article/details/27120605 1 先在cactiserver端安装mysql模板 ...
- ethereumjs/ethereumjs-account-1-简介和API
https://github.com/ethereumjs/ethereumjs-account Encoding, decoding and validation of Ethereum's Acc ...