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 ...
随机推荐
- dbms_randon package
reference to wbsite:http://zhangzhongjie.iteye.com/blog/1948930#comments DBMS_RANDON PACKAGE: Define ...
- 7za 命令解析
转载自:blog.chinaunix.net/uid-26330274-id-3055157.html 7za 命令讲的很详细,收藏下来. 命令行压缩解压一 7z 1) 简介 7z,全称7-Zip ...
- Yii框架记录
Yii框架记录 Yii 结构 使用yii开发一段时间,发现自身知其形不知其意,重温了下yii,理解框架,也可以梳理自己的知识库,借鉴成长,阶段性总结如下: 模型 模型是MVC模式中的一部分,是表现业务 ...
- 打通 Spark 系统运行内幕机制循环流程
本课主题 打通 Spark 系统运行内幕机制循环流程 引言 通过 DAGScheduelr 面向整个 Job,然后划分成不同的 Stage,Stage 是从后往前划分的,执行的时候是從前往后执行的,每 ...
- C/C++ 合法整数与字符
一.C语言中的合法整型 首先C语言中的整型有三种表示方式:十进制.八进制和十六进制.(C语言中没有表示二进制的整型) 十进制: 如 int a = 63; //一个正常的整型 八进制: 如果想用8进制 ...
- SAP Cloud for Customer Account和individual customer的区别
在SAP Cloud for Customer的Customers工作中心里,有三个视图:Accounts,Contacts和Individual Customers. 这三种主数据的区别是什么?我们 ...
- 关于一篇对epoll讲的比较好的一篇文章
原文地址http://www.cnblogs.com/lojunren/p/3856290.html 前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是select和poll,自Li ...
- django get_object_or_404
django get_object_or_404 是django shortcuts模块里面一个比较简便的方法,特别是用django get来操作数据库的时候,可以帮 我们少写一些代码,加快开发速度. ...
- 博客改用markdown编写啦!
如题,在某谷上交了一篇题解之后,顿时觉得\(markdown\)非常好看.
- 认识Jmeter操作界面
使用工具:Jmeter(版本apache-jmeter-2.13) 安装前提:JDK的安装. 主要对GUI操作界面的讲解 (http://jmeter-plugins.org/downloads/al ...