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 ...
随机推荐
- Linux->Ubuntu下配置telnet环境
1.首先查看telnet运行状态 netstat -a | grep telnet 输出为空,表示没有开启该服务 2.安装openbsd-inetd apt-get install openbsd-i ...
- Python学习---匿名函数和闭包的学习
1.1. 匿名函数 匿名函数的命名规则: 用lamdba 关键字标识,冒号(:)左侧表示函数接收的参数(a,b) ,冒号(:)右侧表示函数的返回值(a+b). 因为lamdba在创建时不需要命名,所 ...
- spring core
https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans
- 科普文:从人人网看网络科学(Network Science)的X个经典问题
转:https://zr9558.wordpress.com/2013/12/05/科普文:从人人网看网络科学(network-science)的x个经典问/ 长文,写了N个小时写完的.你肯定能看懂, ...
- C#图解教程读书笔记(第6章 类进阶)
类成员声明语句由下列部分组成:核心声明.一组可选的修饰符和一组可选的特性(attribute). [特性] [修饰符] 核心声明 修饰符: 如果有修饰符,必须放在核心声明之前. 如果有多个修饰符,要有 ...
- App上线流程全攻略(史上最具体步骤)
@转载请保留:iOS界一迷糊小书童--->专注于iOS开发!!谢谢合作 /*****************************************1****************** ...
- BZOJ1002:[FJOI2007]轮状病毒(找规律,递推)
Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子 和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下 ...
- luogu P3941 入阵曲
嘟嘟嘟 这道题我觉得跟最大子矩阵那道题非常像,都是O(n4)二维前缀和暴力很好想,O(n3)正解需要点转化. O(n4)暴力就不说啦,二维前缀和,枚举所有矩形,应该能得55分. O(n3)需要用到降维 ...
- HDU 6386 Age of Moyu 【BFS + 优先队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...
- 20145223 杨梦云 《网络对抗》 Web安全基础实践
20145223 杨梦云 <网络对抗> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 **原理**:SQL注入攻击是通过构建特殊的输入作为参数传入web应用程 ...