LeetCode(306) Additive Number
题目
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;
}
};
LeetCode(306) Additive Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
- LeetCode(9)Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
随机推荐
- 练习三十:Python回文数判断编程练习。
说到回文数,大家可能会比较的陌生,但是在我们的日常生活中常会遇到这样的数字,只是你不知道它是回文数罢了. 例如:12321,这组数字就是回文数. 设n是一任意自然数.若将n的各位数字反向排列所得自然数 ...
- 去掉word文档两边的空白
1.设置-页面布局-页边距,把左边距和右边距的数据设置到最小就好,一般为0.43CM 2.把WORD页面顶部标尺,左右拉到最底,如图: 3.在打印预览里,设置页边距,操作方法同 上述 1,如图:
- Python使用selenium进行爬虫(一)
JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...
- 快色排序算法(C语言描述)
快速排序 算法思想 快速排序采用了一种分治策略,学术上称之为分治法(Divide-and-Conquer Method). 哨兵(如下算法中的key) 每趟排序将哨兵插入到数组的合适位置,使得哨兵左侧 ...
- 常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化
import android.content.Context; import android.content.res.AssetManager; import android.content.res. ...
- ztree的CheckBox不显示问题解决办法
问题: 在使用ztree插件时需要设置 zTree 的节点上是否显示 checkbox / radio,但设置后不显示复选框/单选框,如下图所示 设置方法: var setting = { check ...
- C#链接mysql 新手容易出错的问题
1.Access denied for user 'root'@'DESKTOP-AN72KEI' (using password: YES) 出现这个问题的原因是因为mysql的自带用户root理论 ...
- [学习笔记] Markdown语法备忘
Markdown语法总结 标题 # 这是一级标题 ## 这是二级标题 ### 这是三级标题 #### 这是四级标题 ##### 这是五级标题 ###### 这是六级标题 注意#后面要加空格 字体 ** ...
- Context 使用不当造成内存泄露
问题: Activity中的context被传递给了一个生命周期长过activity的对象(通常为静态单实例变量),导致activity不能正常被销毁. 示例:Activity 调用 ChatMgr ...
- sum特殊用法
在python中,list可以存储False和True a = [False] python的sum除了可以加数字,还可以计算列表中False,True的个数,默认是计算False个数 >> ...