题目

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. JDBC和分包

    JDBC(Java Data Base Connectivity,java数据库连接) JDBC是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...

  2. SPEC CPU 使用简介

    SPEC CPU2000简介 SPEC CPU2000是由标准性能评价机构“The Standard Performance Evaluation Corporation (SPEC)”开发的用于评测 ...

  3. ionic 2 起航(一)

    最近的工作项目开始接触Ionic2.学习了一段时间,现在跟大家分享一下. 什么是Ionic 2?     百度一下,ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.cs ...

  4. MySQL-基本概念

    一.Mysql逻辑架构 引用自<高性能Mysql> 二.并发控制 读写锁:读锁是共享的,写锁是排他的,会阻塞其他的写锁和读锁. 锁粒度:表锁.行级锁 三.事务 事务(ACID特性):原子性 ...

  5. MySQL++简单使用记录.md

    #1.简介 MySQL++ is a powerful C++ wrapper for MySQL’s C API. Its purpose is to make working with queri ...

  6. MySQL如何找出未提交事务信息

    前阵子,我写了一篇博客"ORACLE中能否找到未提交事务的SQL语句", 那么在MySQL数据库中,我们能否找出未提交事务执行的SQL语句或未提交事务的相关信息呢? 实验验证了一下 ...

  7. Python之邮件发送

    Python的smtplib提供了一种很方便的途径用来发送电子邮件,它有SMTP协议进行简单的封装,可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下: ...

  8. 扒一扒IT大佬高考:马云数学1分考北大 李彦宏是状元

    http://news.cnblogs.com/n/522622/ 高考今天正式拉开序幕,而像李彦宏.马云等 IT 大佬之前也都参加过高考,他们成绩又都是怎样的呢? 马化腾:放弃天文梦选择计算机 20 ...

  9. COGS 13. 运输问题4

    ★★☆   输入文件:maxflowd.in   输出文件:maxflowd.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     一个工厂每天生产若干商品,需运输到 ...

  10. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...