当前位置: 首页> 教育> 锐评 > 在线长链接转短链接_免费虚拟主机空间互联_b2b免费发布平台_数字营销是干啥的

在线长链接转短链接_免费虚拟主机空间互联_b2b免费发布平台_数字营销是干啥的

时间:2025/7/11 19:39:57来源:https://blog.csdn.net/mobius_strip/article/details/146445203 浏览次数:1次
在线长链接转短链接_免费虚拟主机空间互联_b2b免费发布平台_数字营销是干啥的

Problem

An additive number is a string whose digits can form an 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, return true if it is an additive number or false otherwise.

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

Algorithm

Use DFS to solve the problem, keeping track of the previous two numbers on the path, and continue searching forward if the condition is met.

Code

class Solution:def isAdditiveNumber(self, num: str) -> bool:nlen = len(num)def dfs(s, num1, num2, cnts):if s == nlen:return cnts >= 3for i in range(s, nlen):if s < i and num[s] == '0':breaknum3 = int(num[s:i+1])if cnts >= 2 and num3 != num1 + num2: continueif dfs(i+1, num2, num3, cnts+1):return Truereturn Falsereturn dfs(0, 0, 0, 0)
关键字:在线长链接转短链接_免费虚拟主机空间互联_b2b免费发布平台_数字营销是干啥的

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: