【LeetCode】306. Additive Number

Axure原型模板与元件库APP交互设计素材(附资料) 在Axure中,手机模型是展示APP或小程序界面效果的重要工具。通过导入手机模型元件,设计师可以更加直观地模拟真实设备的显示效果,从而更准确地评估设计的可行性和用户体验。Axure官方及第三方资源库中提供了多种品牌和型号的手机模型,如iPhone、Android等,设计师可以根据项目需求选择合适的模型进行使用。Axure作为一款强大的原型设计工具,为APP和小程序的设计与开发提供了丰富的界面组件元件库和交互设计素材。 阅读详情

题目描述:

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;
    }
};


DICOM图像知识:深入详解DICOM的层级关系 解析DICOM的四个主要层级:Patient(患者)、Study(检查)、Series(序列)和SOP(服务-对象对),探讨它们之间的关系、各自的唯一标识符以及在实际应用中的用途 阅读详情

相关推荐

一种基于频域滤波法消除干扰项与角谱法重构技术的数字全息显微台阶形貌测量实例分析

一种基于频域滤波法消除干扰项与角谱法重构技术的数字全息显微台阶形貌测量实例分析

简单光学 2035

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

Sentaurus TCAD学习——Scheme编程语言

sentaurus scheme语言学习记录

摸摸小熊猫的博客 969

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

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

负雪明烛 2808

【入门AUTOSAR网络管理测试】CANoe测试T_WakeUp时间

随着智能电动汽车行业的快速发展,目前AUTOSAR网络管理也得到了广泛应用,AUTOSAR网络管理测试也是汽车网络测试中非常重要的环节,NM问题导致的汽车ECU不能唤醒、电瓶亏电等问题,对于新入门AUTOSAR网络管理测试的同学不清楚如何开展测试,那么本章节就主要给大家介绍网络管理中T_WakeUp时间如何测试。【入门AUTOSAR网络管理测试】AUTOSAR网络管理规范需求解读。

测试领域技术分享 2143

【LSTM回归预测】attention机制LSTM时间序列回归预测【含Matlab源码 1992期】

attention机制LSTM时间序列回归预测 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 2592

[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

pnpm 卸载教程:彻底移除 pnpm 及全局缓存的方法

卸载 pnpm = 全局包 → CLI → 环境变量 → store 缓存只要按这个顺序来,基本不会留下任何坑。如果你后面还打算重新安装 pnpm / 切换 npm、yarn / 多包管理器共存,这些细节反而更重要,环境一旦干净,后续问题会少很多。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 941

[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

乐鑫科技 EchoEar 喵伴修改自定义唤醒词的方法 EchoEar喵伴、唤醒词修改、ESP32、AI萌宠、智能音箱、开发者指南

标签:乐鑫科技、EchoEar喵伴、唤醒词修改、ESP32、AI萌宠、智能音箱、开发者指南大家好,我是一名前端开发工程师,最近在玩乐鑫科技推出的时,发现它不仅萌趣十足,还能进行全双工语音交互、多模态识别,甚至能识别你的情绪和与你互动。EchoEar 喵伴是由乐鑫科技和火山引擎扣子大模型团队合作开发的,它使用乐鑫的芯片方案(ESP32 系列)+ 火山引擎扣子大模型,支持 WiFi 连接和本地处理,长记忆能力可以让它记住用户的偏好与习惯。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1893

基于 STM32 的手持式安检金属探测器设计与实现

【代码】基于 STM32 的手持式安检金属探测器设计与实现。

weixin_42300449的博客 901

[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

实现端到端加密的WebRTC视频会议终端

需要注意的是,上述示例代码中没有涉及到具体的加密算法和密钥交换过程,而是通过WebRTC API自动处理了加密和密钥交换的细节。然后,我们通过信令服务器将本地SDP发送给对方,并接收对方的SDP。最后,我们将对方的SDP设置为远程描述,并将最终的连接建立起来。最后,我们将对方的SDP设置为远程描述,并建立最终的连接。在本文中,我们将探讨如何使用WebRTC构建一个加密的视频会议终端,并提供相应的源代码。在本文中,我们将探讨如何使用WebRTC构建一个加密的视频会议终端,并提供相应的源代码。

WaJulia的博客 434

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【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

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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、付费专栏及课程。

余额充值