【刷题-LeetCode】306. Additive Number
- 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.
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
Constraints:
numconsists only of digits'0'-'9'.1 <= num.length <= 35
Follow up:
How would you handle overflow for very large input integers?
解
- 回溯法。注意回溯中,在进入分支之后,还要退出分支
- 大数运算。用字符串实现大数的加减乘除
// 大数,只需要重载加法
class BigNum{
public:
string val;
BigNum(string s, bool hasreversed = false){
if(!hasreversed){
reverse(s.begin(), s.end());
}
val = s;
}
void Print(){
for(int i = val.size()-1; i>=0; --i)cout << val[i];
cout << endl;
}
//先加两个字符串公有的部分,再加上剩下的部分
//如果进位不为0,还要加上进位
BigNum operator + (BigNum &b){
string s1 = b.val, res;
int carry = 0, i = 0;
while(i < s1.size() && i < val.size()){
int tmp = carry + (s1[i]-'0') + (val[i]-'0');
carry = tmp / 10;
res += tmp % 10 + '0';
i++;
}
while(i < s1.size()){
int tmp = carry + (s1[i]-'0');
carry = tmp / 10;
res += tmp % 10 + '0';
i++;
}
while(i < val.size()){
int tmp = carry + (val[i]-'0');
carry = tmp / 10;
res += tmp % 10 + '0';
i++;
}
if(carry)res += carry + '0';
return BigNum(res, true);
}
};
class Solution {
public:
bool isAdditiveNumber(string num) {
vector<BigNum>path;
return dfs(num, 0, path);
}
bool dfs(string &num, int pos, vector<BigNum>&path){
//结束条件,当pos到达了字符串末尾,则枚举完毕,看path的长度,如果大于2,说明前面的枚举是成功的
if(pos >= num.size())return path.size() >= 3;
int len = path.size();
// 如果len<2,说明最前面的两个数字还没有选出来,枚举选择即可
// 不能出现前缀0,即02 00这些是不合法的,只能选成0
if(len < 2){
for(int i = 1; i <= num.size(); ++i){
if(num[0] == '0' && i > 1)break;
path.push_back(BigNum(num.substr(0, i)));
for(int j = i+1; j <= num.size(); ++j){
if(num[i] == '0' && j > i + 1)break;
path.push_back(BigNum(num.substr(i, j-i)));
if(dfs(num, j, path)){
return true;
}else{
path.pop_back();
}
}
path.pop_back();
}
}else{
// 算出前两个数字的和,看对应的字符串是不是num的子串,不是的话返回,是的话往后搜索
BigNum next_int = path[len-2] + path[len-1];
string next_str = next_int.val;
reverse(next_str.begin(), next_str.end());
if(num.substr(pos, next_str.size()) == next_str){
path.push_back(next_int);
bool res = dfs(num, pos + next_str.size(), path);
path.pop_back();
return res;
}else{
return false;
}
}
return false;
}
};
【刷题-LeetCode】306. Additive Number的更多相关文章
- [LeetCode] 306. Additive Number [Medium]
306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...
- Leetcode 306. Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- 【刷题-LeetCode】200 Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【刷题-LeetCode】191 Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and return the number of '1' bits i ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- 306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- 306 Additive Number 加法数
Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
随机推荐
- Ribbon——负载均衡
一.什么是Ribbon Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超 ...
- HTML标签一览
html标签属性大全 嵌套的html窗口<iframe > <iframe src="https://www.baidu.com"></iframe& ...
- 一个VScode主题网站
分享一个我常用的VScode主题网站 https://vscodethemes.com/dark 它提供了 深色和浅色主题供大家选择 查看主题插件 下载 深色 浅色
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- Visible Trees(hdu2841)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Glossary Collection
目录 直接修饰用 间接强调用 (多为副词) 过渡用 特别的名词 动词 词组 各种介词 句子 摘要 引言 总结 正文 实验 直接修饰用 Word 含义 例句 近义词 nuanced adj. 微妙的:具 ...
- PowerDotNet平台化软件架构设计与实现系列(11):日志平台
所有后端应用几乎都会记录日志,日志系统可以统一抽象出来提供服务. 最近被Log4j2的安全漏洞刷屏了,作为开发人员的我只能咩哈哈几次表示日志处理太难了,只有折腾过的人才知道这里面的艰辛啊. 在实现Po ...
- [C++]C++ STL库函数大全
#include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include <errno.h> //定义错误码 # ...
- Java Web程序设计笔记 • 【第10章 JSTL标签库】
全部章节 >>>> 本章目录 10.1 JSTL 概述 10.1.1 JSTL 简介 10.1.1 JSTL 使用 10.1.2 实践练习 10.2 核心标签库 10.2. ...
- Java初学者作业——声明变量储存商品信息并进行输出
返回本章节 返回作业目录 需求说明: 声明变量存储商品信息(商品名称.商品价格和商品库存数量). 输出商品信息. 实现思路: 打印商品商品信息实现步骤: 声明变量存储商品信息.为变量赋值. 输出变量的 ...