【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 ...
随机推荐
- centos修改无法用用户名和密码登录
vi /etc/ssh/sshd_configPermitRootLogin这行改为PermitRootLogin yesPasswordAuthentication no上面的no改为yesUseP ...
- TCP协议随笔
传输控制协议TCP是面向连接.保证高可靠性(数据无丢失.数据无失序.数据无错误.数据无重复到达)传输层协议.TCP/IP结构对应OSITCP/IP ...
- 10.Java 加解密技术系列之 DH
Java 加解密技术系列之 DH 序 概念 原理 代码实现 结果 结束语 序 上一篇文章中简单的介绍了一种非对称加密算法 — — RSA,今天这篇文章,继续介绍另一种非对称加密算法 — — DH.当然 ...
- 开涛spring3(4.1) - 资源 之 4.1 基础知识
4.1.1 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源 (JBoss AS 5.x上的VFS资源)等 ...
- how to install Web logic Server (WLS)
a) Download artificts from: http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main ...
- Ultimus BPM 金融与证券行业应用解决方案
Ultimus BPM 金融与证券行业应用解决方案 行业应用需求 金融服务业的整合与全球化发展,带来高度竞争的国际市场,所牵涉的产业包括了商业.贷款.投资银行,以及保险公司和许多其它为企业和消费者提供 ...
- 矢量量化(VQ)
作者:桂. 时间:2017-05-31 21:14:56 链接:http://www.cnblogs.com/xingshansi/p/6925955.html 前言 VQ(Vector Quant ...
- 将 FFmpeg 移植到 Android平台 (完整版)
首先需要去FFmpeg的官网http://www.ffmpeg.org/去下载FFmpeg的源码,目前的版本号为FFmpeg3.3(Hilbert). 下载的文件为压缩包,解压后得到ffmpeg-3. ...
- java将类和函数封装成jar,然后在别的项目中使用这个jar包
本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...
- Java 9 揭秘(3. 创建你的第一个模块)
文 by / 林本托 Tips 做一个终身学习的人. 在这个章节中,主要介绍以下内容: 如何编写模块化的Java程序 如何编译模块化程序 如何将模块的项目打包成模块化的JAR文件 如何运行模块化程序 ...