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的更多相关文章

  1. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

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

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  3. Leetcode 306. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  4. [LeetCode] 306. Additive Number [Medium]

    306. Additive Number class Solution { private: string stringAddition(string &a, string &b) { ...

  5. 【LeetCode】306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  6. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  7. 【刷题-LeetCode】306. Additive Number

    Additive Number Additive number is a string whose digits can form additive sequence. A valid additiv ...

  8. 306. Additive Number

    题目: Additive number is a string whose digits can form additive sequence. A valid additive sequence s ...

  9. [Swift]LeetCode306. 累加数 | Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

随机推荐

  1. Linux更改主机名的最简单方法

    之前写过一篇关于CentOS更改主机名的随笔,搞得很复杂,详见修改阿里云CentOS Linux服务器的主机名. 今天在askubuntu上发现一个很简单的方法(How do I change the ...

  2. 第十八章:Android 打包部署

    Andriod应用程序如果要在手机或模拟器上安装,必须要有签名! 1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序, ...

  3. 如何在C#中生成与PHP一样的MD5 Hash Code

    最近在对一个现有的系统进行C#改造,该系统以前是用PHP做的,后台的管理员登陆用的是MD5加密算法.在PHP中,要对一个字符串进行MD5加密非常简单,一行代码即可: md5("Somethi ...

  4. SQL——字符串处理函数

    1) ASCII Format:ASCII ( character_expression ) Function:返回表达式最左端字符的ASCII值. eg: select ASCII('abcdef' ...

  5. java web面试题,收集

    java面试题: http://www.codeceo.com/article/java-interview-question.html(很多题都很废) http://www.php100.com/h ...

  6. JAVA学习AWT绘图

    package com.graphics; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel ...

  7. pl/sql死锁oracle

    http://jingyan.baidu.com/album/3ea51489eb65b152e61bba8b.html?picindex=2

  8. js异步加载的三种解决方案

    默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很大的javascript,如果放在页 ...

  9. android: 播放视频

    播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView 类来实现的.这个 类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器. VideoView 的用法和 ...

  10. 《软件性能测试与LoadRunner实战教程》新书上市

    作者前三本书<软件性能测试与LoadRunner实战>.<精通软件性能测试与LoadRunner实战>和<精通软件性能测试与LoadRunner最佳实战>面市后,受 ...