306. Additive Number

class Solution {
private:
string stringAddition(string &a, string &b) {
int l1 = a.size(), l2 = b.size();
int len = max(l1, l2) + 2;
char str[len];
auto i1 = a.crbegin(), i2 = b.crbegin();
int i = 0, carry = 0;
bool b1 = (i1 != a.crend()), b2 = (i2 != b.crend());
while (b1 || b2) {
int da = 0, db = 0;
if (b1) da = *(i1++) - '0';
if (b2) db = *(i2++) - '0';
int d = da + db + carry;
carry = d / 10;
d %= 10;
str[i++] = '0' + d;
b1 = (i1 != a.crend()), b2 = (i2 != b.crend());
}
if (carry) {
str[i++] = '0' + carry;
}
str[i] = '\0';
for (int j = 0, k = i - 1; j < k; ++j, --k) {
swap(str[j], str[k]);
}
return string(str);
}
public:
bool isAdditiveNumber(string num) {
int n = num.size();
for (int l1 = 1; l1 <= n; ++l1) {
for (int l2 = 1; l2 <= n; ++l2) {
int end = l1 + l2;
if (end >= n) break;
string a = num.substr(0, l1);
string b = num.substr(l1, l2);
while (end != n) {
string c = stringAddition(a, b);
if (num.substr(end, c.size()) != c) break;
a = b;
b = c;
end += c.size();
}
if (end == n) {
return true;
}
}
}
return false;
}
};
// 0ms

算法思路: 字符串加法 + 回溯

时间复杂度: O(n^3)

空间复杂度: O(n)

[LeetCode] 306. Additive Number [Medium]的更多相关文章

  1. Leetcode 306. Additive Number

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

  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 s ...

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

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

  5. 306. Additive Number

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

  6. 306 Additive Number 加法数

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

  7. [LeetCode] Super Ugly Number (Medium)

    Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...

  8. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  9. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. [转]jQuery选择器总结

    该文章转载自:http://www.cnblogs.com/onlys/articles/jQuery.html jQuery的选择器那绝对最强大的,各种你想不到,原先想总结一下,没想到搜索到这个比我 ...

  2. git研究1

    error:src refspec master does not match any  将本地GIT版本库PUSH到一个GITHUB上一个空的版本库时出现错误,本地版本库为空, 空目录不能提交 (只 ...

  3. 推荐几本C#程序员阅读的书籍

    http://www.cnblogs.com/tongming/p/3879752.html

  4. 浅析JAVA设计模式(二)

    2. 里氏替换原则:LSP(Liskov Substitution Principle)里氏替换原则,定义为只要父类出现的地方子类就可以出现,而且用子类替换后,程序也不会出现问题,使用者根本不用关心是 ...

  5. 安装hadoop多节点 各种整理

    ubuntu烧制usb启动盘链接: 点击打开链接https://help.ubuntu.com/community/Installation/FromUSBStick ubuntu磁盘分区: 点击打开 ...

  6. HTML XML XHTML DHTML区别与联系

    (1)HTML HTML是超文本标记语言 (2)XML XML是可扩展标识语言,但XML是一种能定义其他语言的语. XML最初设计的目的是弥补HTML的不足, 以强大的扩展性满足网络信息发布的需要 , ...

  7. html5 canvas绘制圆形印章,以及与页面交互

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Linux grep和find的区别

    这是两个不同的命令,关于grep:Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...

  9. thinkphp+mysql+bootstrap

    #thinkphp+mysql+bootstrapthinkphp3.2.3,bootstrap V3一个简易的企业cms网站系统,只要将clients的host改为对应的域名即可.thinkphp. ...

  10. 005 Python的数值类型

    005 Python的数值类型 BIF    指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...