[LeetCode] 306. Additive Number [Medium]
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]的更多相关文章
- Leetcode 306. Additive Number
Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...
- 【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 s ...
- 【刷题-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 ...
- 306 Additive Number 加法数
Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- 【leetcode】Single Number (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- C#版 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- C#中out和ref使用
1.out必须在方法中为其赋值,在调用的时候必须在变量的前面加上out关键字,侧重输出. 2.ref修饰方法的参数,在调用的时候必须在变量的前面加上ref关键字,可以修改其值也可以不修改,侧重修改. ...
- .net开发---自定义页面打印区域
自定义页面打印区域 有3种办法: 办法一:将不需要打印的部位隐藏掉 Examp: <%-- (1)使用css样式,定义一个.noprint的class,将不打印的内容放入这个class内. -- ...
- NewtonSoft.json 序列化和反序列化实例
在百度 API Store 找个旅游的 API 来当成本次 Demo 的例子 接口地址:http://apis.baidu.com/apistore/attractions/spot AIPKEY: ...
- Oracle初始化
/*第1步:创建临时表空间 */ create temporary tablespace mdb_temp tempfile 'G:\data\oracle\mdb_temp.ora' size 10 ...
- Swift 学习手记1,pod 的 类库使用
问题: 在Swift中,我们无法使用像Objective-c 一样的 #import 例如 在头部输入 #import <ReactiveCocoa/ReactiveCocoa.h> 是不 ...
- 反射 介绍System.Type类
本节先介绍system.Type类,通过这个类可以访问关于任何数据类型的信息. 1. system.Type类以前把Type看作一个类,但它实际上是一个抽象的基类.只要实例化了一个Type对象,实际上 ...
- 【制作镜像】BCEC制作镜像
如要制作的新镜像已存在标准版本镜像,即linux发行版本相同(此处指CentOS6.5 64位),可利用BCEC制作. 在BCEC创建centos6.5系统的可联外网的虚机,ssh到此虚机,用yum方 ...
- C语言程序设计概述
1 概论 1972年Dennis Ritchie发明了C语言,而后Dennis Ritchie又使用C语言重写了Unix系统,自那以后C语言逐渐受到了全世界大多数编程爱好者的喜爱,后期的主流操作系统L ...
- Pyhon编码事项
1. 永远不要使用import * Pylint代码审查:Wildcard import XXX 如果函数名重名,或者要导入的内容里面包含了from datetime import datetime, ...
- IOS 多线程之GCD
参考:http://www.cnblogs.com/wendingding/p/3806821.html <<Objective-C基础教程>> 第二版 一 简介 GCD 全称 ...