【LeetCode】306. Additive Number

中科蓝讯(第一次使用指南) 中科蓝讯 新手指南 阅读详情

题目描述:

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.

For example:
"112358" is an additive number because 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
"199100199" is also an additive number, the additive sequence is: 1, 99, 100, 199.
1 + 99 = 100, 99 + 100 = 199

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

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

即给定一个由数字组成的字符串, 将这个字符串切割为至少三个部分并构成数字(不能包含前导0), 使得除了前两个元素外,每个元素等于它的前两个元素之和.


解题思路:

题目的主要困难点在于如何确定分割点.

我使用了递归的方式, 将大问题分割为小问题.


定义函数

bool exist(int i, int j,int k,vector<int>& a,vector<int>& b,string &s);

记a+b=c

其中a为字符串[i,j)构成的数字

b为字符串[j,k)构成的数字

此时求和得到c

再从下标c开始, 判断子符串[k,x)是否能构成数字c

若能构成数字c, 那么下一次的判断是a: [j, k), b:[k, x),

递归的结束条件为x == s.size(), 也就是字符串最后一个位置+1


初始化的时候, 枚举j和k的值即可, 判断是否能够构成Additive Number.

算法的时间复杂度为O(n^3).


参考代码:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution { 
public:
    bool exist(int i, int j,int k,vector<int>& a,vector<int>& b,string &s){
        if (k == s.size())return true;
        //a+b=c
        //a belong to s[i,j)
        //b belong to s[j,k)
        vector<int> c;
        int carry = 0;
        for (int ii=0,jj=0;carry||ii<a.size()||jj<b.size();++ii,++jj){
            if (ii < a.size())carry += a[ii];
            if (jj < b.size())carry += b[jj];
            c.push_back(carry % 10);
            carry /= 10;
        }
        int t = k;
        for (int y = c.size()-1;y >= 0;--y){
            if (t >= s.size())return false;
            int h = s[t++] - '0';
            if (h != c[y])return false;
        }
        return exist(j,k,k+c.size(),b,c,s);
    }
    vector<int> getNum(int i, int j, string &s){
        //[i,j)
        vector<int> v;
        for (int k = j-1;k>=i;--k){
            v.push_back(s[k] - '0');
        }
        return v;
    }
    bool isAdditiveNumber(string num) {
        if (num.size() < 3)return false;
        for (int j = 1;j < num.size();++j){
            for (int k = j+1;k < num.size();++k){
				if (num[j] == '0' && (k-j)>1)break;
				vector<int> a = getNum(0,j,num);
				vector<int> b = getNum(j,k,num);
                if (exist(0,j,k,a,b,num)){
                    return true;
                }
            }
        }
        return false;
    }
};


影楼精修-高低频磨皮算法解析 本文介绍了像素蛋糕软件中的高低频磨皮算法原理及实现思路,抛砖引玉。 阅读详情

相关推荐

BDD测试之Behave使用入门

BDD测试(Behavior-Driven Development Testing)是一种基于“行为驱动开发”(BDD)理念。

想要成为酷盖但是失败的小明 583

Additive Number | Leetcode

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

216

DRV8718引脚功能全解析:56引脚VQFN封装设计精髓

本文深入解析了DRV8718-Q1的56引脚功能及其在汽车座椅控制系统中的应用。重点分析了SPI通信接口、PWM输入控制、电流检测放大器和栅极驱动等关键区域,详细介绍了各引脚的功能特性、电气参数和设计要点。同时提供了电源去耦、PCB布局和信号完整性等最佳实践建议,并以8向电动座椅为例展示引脚配置方案。文章为工程师理解该芯片的引脚设计逻辑和系统应用提供了实用参考。

VehSwHwDeveloper的博客 1207

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

【LeetCode】306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/additive-number/...

负雪明烛 2808

手把手教你用STM32CubeMX和HAL库驱动DW3000:从SPI配置到第一个测距Demo

本文详细介绍了如何使用STM32CubeMX和HAL库驱动DW3000 UWB定位通信模块,从SPI配置到实现厘米级测距Demo的全过程。涵盖硬件连接、SPI通信调试、驱动移植及双向测距(TWR)算法实现,帮助开发者快速掌握UWB高精度定位技术,适用于仓储物流、智能家居等场景。

weixin_27038245的博客 485

[SoC]SoC验证中bootcode与firmware相关验证经验总结

本文总结了SoC验证中bootcode和firmware的验证经验。重点介绍了两种数据加载方法:1)使用Verilog系统函数$readmemh/$readmemb直接写入ROM/ARAM,需注意文件路径、格式和斜杠方向;2)通过Memory Hierarchy在initial块中操作,需要添加适当延时并注意路径拼接。文章还提供了在testcase phase中先读取数据到HASH再写入SRAM的示例,并强调加载后需使能edc_bypass以避免ECC错误。验证过程中需特别注意文件位置、格式要求及路径处理等

元直的博客 2498

[leetcode] 306. Additive Number

题目: 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

u014673347的专栏 1098

FPGA/IC 秋招笔试/面试题总结(续)

FPGA/IC 秋招笔试/面试题总结(续)来啦… 文章目录二十七、状态机1. Moore/Mealy状态机2.一、二、三段式状态机 二十七、状态机 1. Moore/Mealy状态机 (1)Moore状态机: 输出只由当前状态决定,即次态=f(现态。输入),输出=f(现态) Moore型状态机的输出信号是直接由状态寄存器译码得到的。 Moore型状态机检测序列1101如下 Moore型状态机输出函数只由状态变量决定,想要输出Z=1,必须S4状态形成,即寄存器中的1101都打进去后才可以。输出Z=1会在

Crazzy_M的博客 2万+

[leetcode] 306. Additive Number 解题报告

题目链接: https://leetcode.com/problems/additive-number/ Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Excep

小榕流光的专栏 6124

SAP SLT 在 carve-out 场景中的关键作用:从理论到实操全景展现

深入剖析 SAP Landscape Transformation(SLT Replication Server)在 carve‑out 场景中的价值,需要从 carve‑out 本身的背景逻辑说起。carve‑out 指的是企业因并购、拆分、剥离商业单元等因素,而将 SAP 系统中某部分业务(例如组织单元、公司代码、模块数据等) separated 出来,导入到独立的系统中。这一过程极具挑战性,既要确保业务连续性,又要实现数据精准迁移和系统隔离 (

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 7554

多后端系统统一聚合与智能分流:用 Multi Origin Composition 与 Routing 打造一套可扩展的 SAP Gateway 架构

本文针对企业级SAP集成场景,详细介绍了如何利用SAP Gateway的MOC(多源组合)和Routing功能实现多后端系统的智能路由与数据聚合。通过系统别名配置、用户角色绑定和主机名分流等机制,可实现按用户、按域名的精确路由。文章还提供了配置清单、性能优化建议和常见问题解决方案,帮助工程师落地多后端OData服务的最佳实践。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 187

[LeetCode306] Additive Number

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

coder 进阶的专栏 3345

点云目标检测:open3d多窗口联动可视化

多个模型之间的对比除了指标之外,还需要可以直接直观的对比可视化结果,这次介绍一下如何使用open3d同时打开两个窗口分别加载两个模型的结果进行对比,同时实现两个窗口的联动,也就是当一个窗口拖动或者缩放时另外一个窗口也跟着做同样的变换。

zhaoguanhua的博客 4688

LeetCode 306. Additive Number

LeetCode 306. Additive Number Description Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three num...

weixin_34102807的博客 86

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode——306——Additive Number

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 s

happyxuma1991的博客 311

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【Leetcode】306. Additive Number

题目地址: https://leetcode.com/problems/additive-number/ 给定一个数字组成的字符串sss,问其是否能拆成若干子串之拼接s=s1+s2+...+sks=s_1+s_2+...+s_ks=s1​+s2​+...+sk​,使得若将sis_isi​看作数字,有si=si−1+si−2s_i=s_{i-1}+s_{i-2}si​=si−1​+si−2​。不允许有前导000,并且k>2k>2k>2。 思路是,先枚举前两个数,只要前两个数确定了,后面能不能

数学、算法爱好者的博客 275

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Leetcode 306. Additive Number

【代码】Leetcode 306. Additive Number。

小白菜又菜 500

LeetCode: 306. Additive Number

LeetCode: 306. Additive Number 题目描述 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 numbe...

杨领well的专栏 246

LeetCode:306. Additive Number

原题: 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 t

x_i_y_u_e的专栏 2489

LeetCode(306) Additive Number

题目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 se

指尖飞舞 2585

leetcode [306]Additive Number

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 s...

weixin_30827565的博客 100
上一篇: 【LeetCode】220. Contains Duplicate III
下一篇: 【LeetCode】337. House Robber III
枫叶果酱
博客等级 码龄10年 1粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值