题目

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

A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.

For example:

“112358” is an additive number because the digits can form an additive sequence: 1, 1, 2, 3, 5, 8.

1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8

“199100199” is also an additive number, the additive sequence is: 1, 99, 100, 199.

1 + 99 = 100, 99 + 100 = 199

Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.

Given a string containing only digits ‘0’-‘9’, write a function to determine if it’s an additive number.

分析

给定一个字符串,题目要求判断该字符串是否为约束规则下的可加字符串。

手动判断很容易,但是转为程序实现开始不知从何入手。

查阅一些资料,最终搞定。详见代码。

AC代码

class Solution {
public:
bool isAdditiveNumber(string num) {
if (num.empty())
return false; int len = num.size();
for (int i = 1; i < len - 1; ++i)
{
string a = num.substr(0, i); //非首个以0开头的加数违反规则
if (a[0] == '0' && i > 1)
continue; for (int j = 1; j < len; ++j)
{
string b = num.substr(i, j);
if (b[0] == 0 && j > 1)
continue;
string ret = add(a, b);
if (i + j + ret.length() > len)
continue; //存储原字符串中和上一和 同样长度的子串
string val = num.substr(i + j, ret.length()); //当前已经相加的末尾下标
int pass = i + j; string tmp;
while (ret == val)
{
//判断是否到字符串末尾
pass += val.length();
if (len == pass)
return true;
tmp = b;
b = ret;
//下一步骤加法实现
ret = add(tmp, b);
val = num.substr(pass, ret.length());
}//while
}//for
}//for
return false;
} //字符串加法实现
string add(string a, string b)
{
int len_a = a.size(), len_b = b.size(); string ret = "";
int i = len_a - 1, j = len_b - 1, carry = 0; while (i>=0 && j>=0)
{
int tmp = a[i] + b[j] - 2 * '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0'; ret += c;
--i;
--j;
}//while while (i >= 0)
{
int tmp = a[i] - '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0'; ret += c;
--i;
}//while while (j >= 0)
{
int tmp = b[j] - '0' + carry;
carry = tmp / 10;
char c = tmp % 10 + '0'; ret += c;
--j;
}//while if (carry != 0)
ret += carry + '0'; reverse(ret.begin(), ret.end()); return ret;
}
};

GitHub测试程序源码

LeetCode(306) Additive Number的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  3. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  4. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  5. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  6. LeetCode(136) Single Number

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

  7. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  8. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. 关于使用rancher部署k8s集群的一些小问题的解决

    问题一: 在rancher的ui上,不能创建k8s的master节点的高可用集群.创建k8s集群,添加节点的时候,可以添加多个master,但是多个master又没有高可用,只要其中一个出问题了,那么 ...

  2. reaver 破解wifi

    1. 无线网卡设置为监控模式 airmon-ng start wlan0 2. 获取附近路由信息 airodump-ng wlan0mon 3. 使用reaver破解wifi reaver -i wl ...

  3. Linux下无图形界面安装Matlab

    1 下载R2015b_glnxa64.iso和破解文件Matlab+2015b+Linux64+Crack 百度网盘可以直接搜索资源.推荐一个可以多线程下载百度网盘超大文件的工具Aria2,均速1.3 ...

  4. 物体检测丨从R-CNN到Mask R-CNN

    这篇blog是我刚入目标检测方向,导师发给我的文献导读,深入浅出总结了object detection two-stage流派Faster R-CNN的发展史,读起来非常有趣.我一直想翻译这篇博客,在 ...

  5. MongoDB 最初级步骤

    对库TEST下的LOG聚集集合中的inserttim字段加索引 步骤(注意:前四步步骤不能错,错了不行): 一,打开F:\mongodb\bin\mongo.exe,也可以用cmd命令指到这个exe执 ...

  6. 【转载】【MVC 学习 Razor语法】

    Razor是MVC3中才有的新的视图引擎.我们知道,在ASP.NET中,ASPX的视图引擎依靠<%和%>来调用C#指令.而MVC3以后有了一套新的使用@标记的Razor语法,使用起来更灵活 ...

  7. 集合、迭代器、增强for循环、泛型

    1集合 集合是java中提供的一种容器,可以用来存储多个数据. 数组的长度是固定的.集合的长度是可变的.集合中存储的元素必须是引用类型数据. 1.1ArrayList集合存储元素(复习) 例: pub ...

  8. JavaScript笔记6-数组新方法

    七.ECMAScript5关于数组的新方法 1.forEach():遍历数组,并为每个元素调用传入的函数;     举例:    var a = [1,2,3]; var sum = 0; //传一个 ...

  9. Collection-Iterator-foreach

    一.Collection(java.util) 1.概述:具有相同性质的一类事物的汇聚的整体,称为集合.任何集合都包含三块内容:对外的接口/接口的实现/对集合运算的算法.         java中使 ...

  10. 织梦list/arclist标签调用文章内容

    list标签: 1. 进入后台->模型表单-> 频道模型 -> 内容模型管理 -> 修改对应的模型 2. 列表附加字段-填写body 3. 调用时添加“addfields='b ...