【LeetCode】306. Additive Number 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/additive-number/description/

题目描述:

Additive number is a string whose digits can form additive sequence.

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

Given a string containing only digits ‘0’-‘9’, write a function to determine if it’s an additive number.

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Example 1:

Input: "112358"
Output: true
Explanation: The digits can form an additive sequence: 1, 1, 2, 3, 5, 8.
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8

Example 2:

Input: "199100199"
Output: true
Explanation: The additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

Follow up:

  • How would you handle overflow for very large input integers?

题目大意

给出了一个有0-9数字组成的纯数字字符串。判断它能不能组成所谓的“加法数字”,即费布拉奇数列。注意这个题注重点在不管你几位数字去划分,只要满足后面的数字等于前两个的和即可。

解题方法

是不是很眼熟呢?和我们刚刚做过的判断是否是有效的IP93. Restore IP Addresses如出一辙:使用回溯法+合理剪枝。

因为只要判断能否构成即可,所以不需要res数组保存结果。回溯法仍然是对剩余的数字进行切片,看该部分切片能否满足条件。剪枝的方法是判断数组是否长度超过3,如果超过那么判断是否满足费布拉奇数列的规则。不超过3或者已经满足的条件下继续进行回溯切片。最后当所有的字符串被切片完毕,要判断下数组长度是否大于等于3,这是题目要求。

代码如下:

class Solution(object):
def isAdditiveNumber(self, num):
"""
:type num: str
:rtype: bool
"""
return self.dfs(num, []) def dfs(self, num_str, path):
if len(path) >= 3 and path[-1] != path[-2] + path[-3]:
return False
if not num_str and len(path) >= 3:
return True
for i in range(len(num_str)):
curr = num_str[:i+1]
if (curr[0] == '0' and len(curr) != 1):
continue
if self.dfs(num_str[i+1:], path + [int(curr)]):
return True
return False

日期

2018 年 6 月 12 日 ———— 实验室上午放假2333刷题吧

【LeetCode】306. Additive Number 解题报告(Python)的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. [LeetCode] 306. Additive Number [Medium]

    306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...

  3. Leetcode 306. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  4. LeetCode 509 Fibonacci Number 解题报告

    题目要求 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, su ...

  5. LeetCode 136 Single Number 解题报告

    题目要求 Given a non-empty array of integers, every element appears twice except for one. Find that sing ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. Linux-root管理员创建新用户

    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...

  2. 学习java的第十七天

    一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...

  3. 日常Java 2021/9/23

    练习使用Math.random函数,以及JOptionPane.showMessageDialog(null,"字符串","Results",JOptionPa ...

  4. day07 ORM中常用字段和参数

    day07 ORM中常用字段和参数 今日内容 常用字段 关联字段 测试环境准备 查询关键字 查看ORM内部SQL语句 神奇的双下划线查询 多表查询前提准备 常用字段 字段类型 AutoField in ...

  5. Flink(九)【Flink的重启策略】

    目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...

  6. Sibel Tools和Siebel Cilent的安装步骤

    关于Siebel的资料在网上是少之又少,当时安装开发工具的时候花了挺长时间的,把步骤记录了下来. 一安装win32_11gR2_client 首先要安装Oracle数据库的客户端,必须是32位,安装过 ...

  7. zabbix之微信报警

    #:先在企业微信注册一个企业微信号 #:注册好之后,进入微信 #:测试一下 #:获取access_token #:开始获取 #:获取 #:在server端安装pip root@ubuntu:~# ap ...

  8. Socket通信和多线程的总结

    1.ServerSocket进行多线程接收 package com.yh.chat; import java.io.IOException; import java.net.ServerSocket; ...

  9. podman wsl2在windows重启后出错

    1. error joining network namespace for container 如果没有先停止容器就重启windows,极大概率就会出现这个问题 解决方法 先停止停止的容器再启动已退 ...

  10. OAuth2.0实战:认证、资源服务异常自定义!

    大家好,我是不才陈某~ 这是<Spring Security 进阶>的第4篇文章,往期文章如下: 实战!Spring Boot Security+JWT前后端分离架构登录认证! 妹子始终没 ...