【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 sequence 1, 2, 03
or 1, 02, 3
is invalid.
Given a string containing only digits '0'-'9'
, write a function to determine if it's an additive number.
Follow up:
How would you handle overflow for very large input integers?
提示:
这题在题目中已经给出了一个重要的提示揭示了题目中的一个小陷阱,由于输入是字符串,所以我们在做加法的时候可能会溢出,所以需要实现一个基于字符串的加法,比如像下面这样:
string add(string a, string b) {
int i = a.length() - , j = b.length() - , carry = ;
string res;
while (i >= || j >= ) {
int digit = carry + (i >= ? a[i--] - '' : ) + (j >= ? b[j--] - '' : );
res.push_back(digit % + '');
carry = digit / ;
}
if (carry) {
res.push_back(carry + '');
}
reverse(res.begin(), res.end());
return res;
}
然后就是要想出算法的整体解决思路,我的思路就是递归求解,一旦发现一个成功的Additive Number就返回true。这里循环内的终止条件就尤为重要了,若可以切割掉一些显然不成立的分支,那么算法的速度就能得到提升。这里的思路是这样的:
- 由于给定的字符串需要划分成至少三个数字,所以第一个数字的长度最多等于字符串长度的一半,因为整除是向下取整,所以取等号:
for (int i = ; i <= num.size() / ; ++i)
- 因为是求和,所以第二个字符串的长度只能最多为剩下字符串长度的一半:
for (int j = ; j <= (num.size()-i)/; ++j)
最后就是递归函数中的终止条件了,这些都比较简单,详见代码。
代码:
class Solution {
public:
bool isAdditiveNumber(string num) {
for (int i = ; i <= num.size() / ; ++i) {
for (int j = ; j <= (num.size()-i)/; ++j) {
if (check(num.substr(, i), num.substr(i, j), num.substr(i+j))) {
return true;
}
}
}
return false;
} bool check(string a, string b, string c) {
if (a.size() > && a[] == '' || b.size() > && b[] == '') {
return false;
}
string r = add(a, b);
if (r == c) {
return true;
}
if (r.size() >= c.size()) {
return false;
}
for (int i = ; i < r.size(); ++i) {
if (r[i] != c[i]) {
return false;
}
}
return check(b, r, c.substr(r.size()));
} string add(string a, string b) {
int i = a.length() - , j = b.length() - , carry = ;
string res;
while (i >= || j >= ) {
int digit = carry + (i >= ? a[i--] - '' : ) + (j >= ? b[j--] - '' : );
res.push_back(digit % + '');
carry = digit / ;
}
if (carry) {
res.push_back(carry + '');
}
reverse(res.begin(), res.end());
return res;
}
};
【LeetCode】306. Additive Number的更多相关文章
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【刷题-LeetCode】306. Additive Number
Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
随机推荐
- 纯真IP数据库格式详解
纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...
- 使用javascript生成当前博文地址的二维码图片
前面的话 在电脑端发现一篇好的博文,想在手机上访问.这时,就必须打开手机浏览器输入长长的URL地址才行,非常不方便.如果在博客标题的后面跟一张小的图片,点击该图片后,出现一张二维码的大图,然后再通过手 ...
- 《Android进阶》之第一篇 在Java中调用C库函数
在Java代码中通过JNI调用C函数的步骤如下: 第一步:编写Java代码 class HelloJNI{ native void printHello(); native void printStr ...
- OpenStack dashboard界面操作 实现登陆虚拟机并通信
1.创建项目,点击"创建项目" (1).填写项目信息 (2).添加与之关联的项目成员 (3).点击"配额",为用户在平台上分配一个操作的空间,便于用户创建网络, ...
- vue+vux+axios+vuex+vue-router的项目的理解
本文主要是讲解项目前期的工作,后期考虑再详细说明. 作为一个技术团队如果你们团队选择了上面的技术栈,这说明你们的技术团体对于vue有很熟练的掌握了.在这里我想说明的是前期架构的重要.这里有一遍博客写的 ...
- hadoop 2.7.3 集群安装
三台虚拟机,centos6.5 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 :: loca ...
- UX2内核浏览加速技术纲要
UX2内核是本人负责主要开发的浏览服务项目,其主要目的是为开发者提供一个简单好用.轻便的网络浏览服务.UX2内核的安卓端是基于WebView进行深度优化的,同时欢迎大家使用这个内核用于app页面或浏览 ...
- PHP实现记录日志(文件)
PHP实现记录日志(文件) php php 记录日志 项目中经常会记录些操作信息,或是打印些关键变量,或者是导入excel文件,提现记录,都需记录.经常遇到,封装一个方法,有不好的地方或补充请留言. ...
- git上传本地项目到github
git软件下载地址:https://git-scm.com/download/ 1. 在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击 ...
- SQL万能语句-经典操作
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...