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. js 字符串编码转换函数

    escape 方法 对 String 对象编码以便它们能在所有计算机上可读, escape(charString) 必选项 charstring 参数是要编码的任意 String 对象或文字. 说明 ...

  2. react中的坑

    9. 渲染界面的时候让滚动条回到顶部 //渲染界面的时候让滚动条回到顶部 componentDidMount() { window.scrollTo(0,0); } 路由: <Route pat ...

  3. 读《编写高质量代码:改善JavaScript程序的188个建议》2

  4. MD5Untils加密工具类

    package com.dzq.utils; import java.math.BigInteger; import java.security.MessageDigest; import java. ...

  5. SQL Server 2012 数据库各个版本功能对比

    作为这篇SQL SERVER 2008数据库各版本功能对比 的姊妹篇,就写点SQL Server 2012 各个版本的区别以及物理以及逻辑上的限制. 个部分来分http://technet.micro ...

  6. HTML_常见命令学习笔记

    1. java类中的这段代码 out.println(" <div class='line'>"); out.println(" <div align= ...

  7. sql查询结果集根据指定条件排序的方法

    oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 有几种办法改变这种情况: (1)用 nvl 函数或decode 函数 将null ...

  8. c编程:提示用户输入一个0—9的数字进行猜测电脑产生的随机数。一共有三次机会。

    // //  main.c //  使用c语言进行编程: 题目:由电脑生成一个由0-9之间的随机数,提示用户也输入一个数字进行猜测.当猜测三次仍不中的时候结束程序. 编译环境:Xcode6.3 特别介 ...

  9. python+sqlite3

    一个小例子, # -*- coding:utf-8 -*- ''' Created on 2015年10月8日 (1.1)Python 2.7 Tutorial Pt 12 SQLite - http ...

  10. LINQ to Entities 查询注意事项

    1> 排序信息丢失 如果在排序操作之后执行了任何其他操作,则不能保证这些附加操作中会保留排序结果.这些操作包括 Select 和 Where 等.另外,采用表达式作为输入参数的 First 和 ...