累加数Python解法
累加数 是一个字符串,组成它的数字可以形成累加序列。
一个有效的 累加序列 必须 至少 包含 3 个数。除了最开始的两个数以外,序列中的每个后续数字必须是它之前两个数字之和。
给你一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是 累加数 。如果是,返回 true ;否则,返回 false 。
说明:累加序列里的数,除数字 0 之外,不会 以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/additive-number
例:
输入:"112358"
输出:true
解释:累加序列为: 1, 1, 2, 3, 5, 8 。1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
解析:
累加序列每一步的操作都是相同的,也就是说只要确定了前两个数就能确定字符串。所以只需要判断前两个值即可,因为前两个数的长度是不确定的,所以可以使用遍历全部获取判断一遍,暴力。
class Solution(object):def isAdditiveNumber(self, num):""":type num: str:rtype: bool"""def check(a, b):if (a.startswith('0') and a != '0') or (b.startswith('0') and b != '0'):return Falsetemp = a + ba, b = b, str(int(b) + int(a)) # a等于b的值,b等于a+b的值temp += b #第一次相加while len(temp) < len(num): # 长度没达到就要一直加下去a, b = b, str(int(b) + int(a))temp += breturn num == temp # 长度达到后进行判断是否相同for j in range(1, len(num) - 1):for i in range(j):if check(num[:i + 1], num[i + 1: j + 1]): # 遍历开始两个数的各种情况return True # 有一种情况符合即为成果return False # 否则失败
累加数Python解法相关推荐
- Python|Leetcode《306》|累加数
专栏<LeetCode|一刷到底> 打卡每天leetcode精选每日一题(尽量不断更!) 点击关注不迷路!!! 一.题目描述 题目:累加数 难度:中等 地址:<306>累加数 ...
- LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归
题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...
- LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法
题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...
- LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
- LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法
题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...
- LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素
题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...
- LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法
题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...
- LeetCode 148. Sort List--面试算法题--C++,Python解法
LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
最新文章
- 移动端重构系列5——等分,居中等
- 完美解决移动Web小于12px文字居中的问题
- 快速搭建CentOS+ASP.NET Core环境支持WebSocket
- BCrypt管理员登录密码验证
- MyBatis操作指南-与Spring集成(基于注解)
- ffmpeg转码_音视频处理神器FFmpeg
- qt5.8连接mysql代码_qt5.8如何连接mysql
- python txt文件排序,使用Python在.txt文件中按數值(降序)排序高分列表
- 原始线性结构单链表的实现以及操作
- 分布式系统中的领导选举
- 红杉中国2021企业数字化年度指南:企业如何制胜数字化浪潮?
- [CTSC2018]暴力写挂
- 在同一台服务器上安装两个数据库
- 3项目里面全局用less变量 cli vue_Vue.js构建工具比较
- 基于Java+SpringBoot+mybatis+vue+element实现旅游管理系统
- python可以下载百度文库_Python在线百度文库爬虫(免下载券)
- 基于javaweb+SpringBoot+MyBatis网上书店管理系统在线购书系统(前台、后台)
- ios 加速计效果实现
- 服务器 虚拟机 崩溃,认识VMware虚拟机,系统崩溃或异常,可通过快照还原磁盘文件系统...
- SveletJs学习——运动动画