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. Unity3D 之UGUI 图片

    这里来降价下Unity3Dl的图片 先创建一个图片 图片的属性 Preserve Aspect -->保持图片的原始宽高比例 Set native Size -->图片原始尺寸 Image ...

  2. 10个你可能不知道的JavaScript小技巧

    1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做 ...

  3. php 5.3 配置mssql笔记

    参考URL  https://docs.moodle.org/29/en/Installing_MSSQL_for_PHP#Using_FreeTDS_on_Debian_Lenny 第一步,下载相应 ...

  4. ReactNative-----环境搭建(android)

    1.参考文档 http://reactnative.cn/docs/0.26/getting-started.html http://reactnative.cn/docs/0.26/running- ...

  5. 【CODECHEF】【phollard rho + miller_rabin】The First Cube

    All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...

  6. ubuntu下编译安装apache

    官网http://httpd.apache.org/download.cgi下载apache源码包后 /*解包*/ gzip -d httpd-2_x_NN.tar.gz tar -xf httpd- ...

  7. mac 下 sublime text 运行c++/c 不能使用scanf/cin

    { "cmd": ["g++", "${file}", "-o", "${file_path}/${file_ ...

  8. qwt6在Windows下Qt5的编译,安装,初步使用

    今晚把qwt的编译,安装,初级使用放上来,以便需要的人,能更快部署好编程环境,不至于每次都像我这样花很多时间. 注意:Qtcreater使用的是什么编译器编译出来的,就要用那个编译器来编译qwt. 我 ...

  9. 青瓷qici - H5小游戏 抽奖机 2 界面布局

    背景图片 首先我们需要在当前场景下面创建UI的根节点,这个根节点决定了我们整个游戏的元素布局,以及适应多分辨率的缩放布局问题,所以我们其他的元素都要放在UIRoot下面. 考虑到我自己测试的时候在PC ...

  10. TDirectory.IsRelativePath是否相对路径

    使用函数: System.IOUtils.TDirectory.IsRelativePath class function IsRelativePath(const Path: string): Bo ...