leetcode 306. Additive Number

3人阅读 评论(0) 收藏 举报
分类:

leetcode 306. Additive Number

题意:给你一个字符串,让你判断这个字符串是否合法。

"112358"合法,因为

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

199100199”合法,因为

1 + 99 = 100, 99 + 100 = 199

注意1:数字没有前导0,所以1,02,3是不合法的。

注意2:数字可能爆int,所以这里用string比较大小。

思路:

1:要确定是否能成立,肯定要爆搜所有点,这里肯定要dfs。那么dfs要怎么搜呢?

bool dfs(int pos, string pre1, string pre2, string now, string num)

pre1是第一个数字,pre2是第二个数字,now是当前数字;

如果pre1+pre2 == now 那么能成立,pre1 = pre2,pre2 = now,继续搜

如果pre1+pre2  > now 那么now往后再加一位再比较

如果pre1+pre2 < now 那么可能不成立,直接返回不行。

2:递归初始条件:

pre1,pre2的初始状态肯定要合法,不能有前导0;

pre1,pre2的状态肯定要全部遍历;

pre1为:0-i;

pre2为: i+1-j;

3: 递归结束条件:

搜到最后一个字符,如果 now == "";返回true

4.字符串大小比较

用三个栈来实现,稍微模拟一下就好。


bool legal(string str)//判断是否合法
bool isAdditiveNumber(string num) //爆搜初始状态的确立

int cmp(string str1, string str2, string str3)//比较str1+str2 和 str3 的大小
bool dfs(int pos, string pre1, string pre2, string now, string num)//爆搜过程
具体代码实现:
class Solution {
public:
	bool legal(string str)
	{
		if (str[0] == '0')
		{
			if (str.size() == 1) return true;
			return false;
		}
		return true;
	}
	bool isAdditiveNumber(string num) {
		if (num.size() <= 2) return false;
		string pre1 = "";
		string pre2 = "";
		for (int i = 0; i<num.size() - 2; i++)
		{
			pre1 = pre1 + num[i];
			pre2 = "";
			if (legal(pre1))
			{
				for (int j = i + 1; j < num.size() - 1; j++)
				{
					pre2 = pre2 + num[j];
					if (legal(pre2))
					{
						bool result = dfs(j + 1, pre1, pre2, "", num);
						if (result)
							return true;
					}
					else
						break;
				}
			}
			else
				break;
		}
		return false;
	}
	int cmp(string str1, string str2, string str3)
	{
		stack<char> num1;
		stack<char> num2;
		stack<char> num3;
		string result = "";
		for (int i = 0; i<str1.size(); i++)  num1.push(str1[i]);
		for (int i = 0; i<str2.size(); i++)  num2.push(str2[i]);
		int flag = 0;
		int num;
		while (!num1.empty() && !num2.empty())
		{
			num = flag + num1.top() - '0' + num2.top() - '0';
			num3.push(num % 10 + '0');
			flag = num / 10;
			num1.pop();
			num2.pop();
		}
		while (!num1.empty())
		{
			num = flag + num1.top() - '0';
			num3.push(num % 10 + '0');
			flag = num / 10;
			num1.pop();
		}
		while (!num2.empty())
		{
			num = flag + num2.top() - '0';
			num3.push(num % 10 + '0');
			flag = num / 10;
			num2.pop();
		}
		if (flag) num3.push(flag + '0');
		while (!num3.empty())
		{
			result += num3.top();
			num3.pop();
		}
		if (result == str3) 
			return 0;
		if (result.size() > str3.size())
			return 1;
		else if (result.size() < str3.size())
			return -1;
		else if (result.size() == str3.size())
			return result > str3?1:-1;
		
	}
	bool dfs(int pos, string pre1, string pre2, string now, string num)
	{
		bool result = false;
		if (pos == num.size())
		{
			if (now == "") return true;
			return false;
		}
		now = now + num[pos];
		if (legal(now))
		{
			int judge = cmp(pre1, pre2, now);
			if (judge > 0)   result = dfs(pos + 1, pre1, pre2, now, num);
			else if (judge == 0)  result = dfs(pos + 1, pre2, now, "", num);
		}

		return result;
	}
};
本来以为我做不出来了,没想做最后的最后还是给我搞出来了!啊哈哈哈!



查看评论

LeetCode(306) Additive Number

题目Additive number is a string whose digits can form additive sequence.A valid additive sequence shou...
  • fly_yr
  • fly_yr
  • 2015-12-12 16:56:33
  • 1996

[LeetCode306] Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • sbitswc
  • sbitswc
  • 2015-12-06 03:56:30
  • 2876

[leetcode] 306. Additive Number

题目: Additive number is a string whose digits can form additive sequence.A valid additive sequence s...
  • u014673347
  • u014673347
  • 2016-02-21 11:29:26
  • 698

LeetCode:306. Additive Number

原题: Additive number is a string whose digits can form additive sequence. A valid additive sequence...
  • x_i_y_u_e
  • x_i_y_u_e
  • 2016-02-23 16:53:20
  • 1284

(Java)LeetCode-306. Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • u012848330
  • u012848330
  • 2016-08-08 20:59:59
  • 207

LeetCode306——Additive Number且看我是如何解决的

Additive number is a string whose digits can form additive sequence. A valid additive sequence sh...
  • booirror
  • booirror
  • 2015-12-05 22:23:38
  • 1651

[leetode-306]Additive Number(java)

问题描述:这里写链接内容问题分析:这道题比较直观,就是采用回溯法。代码如下:2mspublic class Solution { char[] chars; String num; ...
  • zdavb
  • zdavb
  • 2015-11-22 18:46:52
  • 651

LeetCode 306. Additive Number

I first thought about the commented out method. However, that one is hard to split the numbers which...
  • github_34333284
  • github_34333284
  • 2016-05-28 02:27:44
  • 181

306. Additive Number LeetCode

题意:给出一个只包含数字的字符串,问是否可以分成至少3个数,前两个数相加等于第三个数。 题解:枚举前两项,有了前两项就可以递推出后面的值,然后递推到跟原字符串长度相等的时候停止,然后比较是否相等,相...
  • youthinkwu
  • youthinkwu
  • 2016-03-16 17:56:20
  • 231

[LeetCode]--306. Additive Number

See the problem! See some excellent solutions! https://discuss.leetcode.com/topic/29845/python-s...
  • beijingbuaaer
  • beijingbuaaer
  • 2016-08-06 01:34:59
  • 84
    个人资料
    持之以恒
    等级:
    访问量: 8347
    积分: 585
    排名: 9万+
    最新评论