[LeetCode] Additive Number
Af first I read the title as "Addictive Number". Anyway, this problem can be solved elegantly using recursion. This post shares a nice recursive C++ code with handling of the follow-up by implementing string addition function.
The code is rewritten as follows.
class Solution {
public:
bool isAdditiveNumber(string num) {
int n = num.size();
for (int i = ; i <= n/; i++)
for (int j = ; j <= (n-i)/; j++)
if (additive(num.substr(, i), num.substr(i, j), num.substr(i + j)))
return true;
return false;
}
private:
bool additive(string a, string b, string c) {
string s = add(a, b);
if (s == c) return true;
if (c.size() <= s.size() || s.compare(c.substr(, s.size())))
return false;
return additive(b, s, c.substr(s.size()));
}
string add(string& a, string& b) {
string s;
int i = a.size() - , j = b.size() - , c = ;
while (i >= || j >= ) {
int d = (i >= ? (a[i--] - '') : ) + (j >= ? (b[j--] - '') : ) + c;
s += (d % + '');
c = d / ;
}
if (c) s += ('' + c);
reverse(s.begin(), s.end());
return s;
}
};
This problem can also be solved iteratively, like peisi's code, which is rewritten in C++ below.
class Solution {
public:
bool isAdditiveNumber(string num) {
int n = num.length();
for (int i = ; i <= n/; i++)
for (int j = ; max(i, j) <= n - i - j; j++)
if (additive(i, j, num)) return true;
return false;
}
private:
bool additive(int i, int j, string& num) {
if (num[i] == '' && j > ) return false;
string a = num.substr(, i);
string b = num.substr(i, j);
for (int k = i + j; k < num.length(); k += b.length()) {
b.swap(a);
b = add(a, b);
string tail = num.substr(k);
if (b.compare(tail.substr(, b.size()))) return false;
}
return true;
}
string add(string& a, string& b) {
string s;
int i = a.size() - , j = b.size() - , c = ;
while (i >= || j >= ) {
int d = (i >= ? (a[i--] - '') : ) + (j >= ? (b[j--] - '') : ) + c;
s += (d % + '');
c = d / ;
}
if (c) s += ('' + c);
reverse(s.begin(), s.end());
return s;
}
};
If you are a Pythoner, you may also love Stefan's code, which is pasted below.
def isAdditiveNumber(self, num):
n = len(num)
for i, j in itertools.combinations(range(1, n), 2):
a, b = num[:i], num[i:j]
if b != str(int(b)):
continue
while j < n:
c = str(int(a) + int(b))
if not num.startswith(c, j):
break
j += len(c)
a, b = b, c
if j == n:
return True
return False
[LeetCode] Additive Number的更多相关文章
- [LeetCode] Additive Number 加法数
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...
- 【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 shoul ...
- [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 s ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- 【刷题-LeetCode】306. Additive Number
Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...
- 306. Additive Number
题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...
- [Swift]LeetCode306. 累加数 | Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
随机推荐
- 多网卡的7种bond模式原理
多网卡的7种bond模式原理 Linux 多网卡绑定 网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6 常用的有三种 mode=0 ...
- 困扰多日的C#调用Haskell问题竟然是Windows的一个坑
最近一直被C#调用Haskell时的“尝试读取或写入受保护的内存”问题所困扰(详见C#调用haskell遭遇Attempted to read or write protected memory,C# ...
- 修复SharePoint 2013 Search 拓扑错误
Problem 当创建和配置SharePoint 2013 Search Service Application成功之后,进入详细配置页后,在Search Application Topology节点 ...
- ios 设置状态栏文本颜色为白色
1,在.plist文件中添加一个键值对:设置View controller-based status bar appearance的值为NO 2,在方法中 - (BOOL)application:(U ...
- iOS开发之静态库(五)—— 图片、界面xib等资源文件封装到静态框架framework
编译环境:Macbook Air + OS X 10.9.2 + XCode5.1 + iPhone5s(iOS7.0.3) 一.首先将资源文件打包成bundle 由于bundle是静态的,所以可以将 ...
- MYSQL子查询与连接
37:子查询与连接SET 列名 gbk;//改变客户端数据表的编码类型. 子查询子查询(Subquery)是指出现在其他SQL语句内的SELECT子句例如SELECT * FROM t1 WHERE ...
- python web框架——初识tornado
一 Tornado概述 Tornado是FriendFeed使用的可扩展的非阻塞式web框架及其相关工具的开源版本.这个Web框架看起来有些像web.py或者Google的 webapp,不过为了能有 ...
- 正则表达式提取url中的参数,返回json字符串
var urlstr = "www.baidu.com?a=1&b=xx&c"; var s = urlstr.split("?"); var ...
- .NET开发中经常用到的扩展方法
整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1 匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...
- adb uninstall
adb shell pm list packages adb uninstall com.pa.pfac