【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. 商业爬虫学习笔记day7-------解析方法之bs4

    一.Beautiful Soup 1.简介 Beautiful Soup 是python的一个库,最主要的功能是从网页抓取数据.其特点如下(这三个特点正是bs强大的原因,来自官方手册) a. Beau ...

  2. Vue相关,vue.nextTick

    vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...

  3. AOP与IOC的概念

    AOP与IOC的概念(即spring的核心) a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而sp ...

  4. Android 高级UI组件(二)

    1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...

  5. oracle(数据文件)

    --创建数据文件 create tablespace--创建表空间同时创建数据文件 create temporary tablespace --创建临时表空间的同时创建临时数据文件 alter tab ...

  6. maven的lifecycle

    1.maven clean. 清理项目的target目录 2.maven compile 编译项目 3.maven test 编译项目后,再执行Junit测试方法 4.maven package 编译 ...

  7. 【Service】【Web】【Middleware】Tomcat

    1. 概念 1.1. 官方网站:tomcat.apache.org 1.2. tomcat的组件 <Server> <Service> <Connector/> & ...

  8. Redis缓存穿透、缓存击穿以及缓存雪崩

    作为一个内存数据库,redis也总是免不了有各种各样的问题,这篇文章主要是针对其中三个问题进行讲解:缓存穿透.缓存击穿和缓存雪崩.并给出一些解决方案.这三个问题是基本问题也是面试常问问题. 这篇文章我 ...

  9. MySQL批量数据脚本示例

    一.建表 # 新建库 create database bigData; use bigData; #1 建表dept CREATE TABLE dept( id INT UNSIGNED PRIMAR ...

  10. BDD自动化测试框架cucumber(1): 最基本的demo

    BDD(Behavior Driven Development),行为驱动开发, 对应自动化测试框架,python有behave,java有cucumber, 这次记录cucumber+springb ...