306. Additive Number
2016-03-23 14:27
399 查看
Problem
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, 03or
1, 02, 3is invalid.
Given a string containing only digits
'0'-'9', write a function to determine if it's an
additive number.
Follow up:
How would you handle overflow for very large input integers?
Solution
典型的backtracking .....这里直接写了个 addTwoStr ( ) 的方法,避免大数溢出。 如果不考虑大数,就可以把两个字符串变成数字,直接用加号。。。
class Solution { string addTwoStr( const string& opt1, const string& opt2 ){ string rst; int carry = 0; for( int idx1 = opt1.size() - 1, idx2 = opt2.size() -1; idx1 >= 0 || idx2 >= 0 || carry != 0 ; idx1--, idx2-- ){ int curSum = carry; if( idx1 < 0 && idx2 < 0 ){ } else if( idx1 < 0 ){ curSum += ( opt2[idx2] - '0'); } else if( idx2 < 0 ){ curSum += ( opt1[idx1] - '0'); } else if(idx1 >= 0 && idx2 >=0 ){ curSum += ( opt1[idx1] - '0' + opt2[idx2] - '0' ); } carry = curSum/10; curSum %= 10; rst.insert(rst.begin(), curSum + '0'); } return rst; } bool helper( int num1Start, int num2Start, int sumStart, const string& num ){ const int N = num.size(); if ( num1Start >= N || num2Start >= N || sumStart >= N ) return false; string opt1 = num.substr( num1Start, num2Start - num1Start ); string opt2 = num.substr( num2Start, sumStart - num2Start ); if( opt1.size() > 1 && opt1[0] == '0' ) return false; if( opt2.size() > 1 && opt2[0] == '0' ) return false; string goldSum = addTwoStr(opt1, opt2); int goldSize = goldSum.size(); if(num.size() - sumStart < goldSize || goldSum != num.substr( sumStart, goldSize)) return false; if( sumStart + goldSize == num.size() ) { return true; } return helper( num2Start, sumStart, sumStart + goldSize, num ); } public: bool isAdditiveNumber(string num) { const int N = num.size(); for( int num2Start = 1; num2Start < N - 1; num2Start++ ) { for( int sumStart = num2Start + 1; sumStart < N; sumStart++){ if(helper( 0, num2Start, sumStart, num)){ return true; } } } return false; } };
相关文章推荐
- Android中home键和back键区别实例分析
- Android Back键点击两次退出应用详解及实现方法总结
- Android实现长按back键退出应用程序的方法
- Android解决dialog弹出时无法捕捉Activity的back事件的方法
- Android监听Home键和Back键的区别介绍
- Android实现Back功能代码片段总结
- android 按两次返回键退出
- 模拟按键
- android 硬件上的back键处理
- Android重写返回按钮
- 有对话框时,偶尔会捕捉不到返回键
- 320. Generalized Abbreviation
- Android按键拦截处理最佳实践范例(以Back事件为例)
- 点击回退键时出现连续返回的情况
- Tracking-Learning-Detection TLD解析一 - 前言
- Tracking-Learning-Detection TLD解析二 - 跟踪器及检测器设计
- Tracking-Learning-Detection TLD解析三 - Learning学习(跟踪与检测的协调与更新)
- Tracking-Learning-Detection TLD解析四 - 扩展及局限
- 提高增量备份的速度
- App.js添加浏览器返回键支持