码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_67题——Add Binary(字符串string,反向迭代器reverse_iterator,栈stack)

时间:2015-04-20 20:52:08      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

Add Binary

 Total Accepted: 39288 Total Submissions: 158078My Submissions

 

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

 

Hide Tags
 Math String
Have you met this question in a real interview? 
Yes
 
No
 

Discuss

解题思路:

开始没考虑到范围,直接想用整形来表示,发现范围不够,在这种情况下,先写了三个函数,int erjinzhi(int n_2)第n_2位上的数对应着十进制的多少

int string_int(string str)字符串转化为整型,string int_string(int n)整型转化为字符串

#include<iostream>
#include<string>
#include <vector>
#include <stack>
using namespace std;


int erjinzhi(int n_2)
{
	int n_10=1;
	if(n_2==0)
		return n_10;

	for(int i=1;i<=n_2;i++)
		n_10=2*n_10;
	return n_10;
}

int string_int(string str)
{
	int len=str.size();
	int n_10=0;
	for(int i=0;i<len;i++)
	{
		n_10=n_10+(str[i]-48)*erjinzhi(len-1-i);
	}
	return n_10;
}

string int_string(int n)
{
	stack<int> stack_temp;
	int temp=n;
	string str;
	int flag=0;
	if(temp==0)
		str.push_back(‘0‘);
	else
	{
		while(temp>0)
		{
			stack_temp.push(temp%2);
			temp=temp/2;
		}
		while(!stack_temp.empty())
		{
			str.push_back(stack_temp.top()+48);
			stack_temp.pop();
		}
	}
	return str;	
}

string addBinary(string a, string b) {
	return int_string(string_int(a)+string_int(b));
}

  上面这个不能AC的,接下来想到直接用字符串进行计算,从最末尾开始,那是低位,然后采用反向的迭代器来操作数据,而设置了一个

低位到高位的进位,而在每一位得到的数都压到栈里,因为在存储在string中时,又是从高位开始存储的

#include<iostream>
#include<string>
#include<stack>
#include<vector>
using namespace std;
string addBinary(string a, string b) {
	string str;
	stack<char> stac;
	string::reverse_iterator a_end,a_begin,b_end,b_begin;

	if(a.empty())
	{str=b;return str;}
	if(b.empty())
	{str=a;return str;}

	b_end=b.rbegin();
	a_end=a.rbegin();
	string::reverse_iterator j=b.rbegin();
	string::reverse_iterator i=a.rbegin();
	int jinwei=0;
	for(;i!=a.rend()&&j!=b.rend();i++,j++)
	{
		if((*i-48)+(*j-48)+jinwei==0)
		{
			jinwei=0;
			stac.push(‘0‘);
		}
		else if((*i-48)+(*j-48)+jinwei==1)
		{
			jinwei=0;
			stac.push(‘1‘);
		}
		else if((*i-48)+(*j-48)+jinwei==2)
		{
			jinwei=1;
			stac.push(‘0‘);
		}
		else if((*i-48)+(*j-48)+jinwei==3)
		{
			jinwei=1;
			stac.push(‘1‘);
		}
	}
	if(i!=a.rend()&&j==b.rend())
	{
		for(;i!=a.rend();i++)
		{
			if(*i-48+jinwei==0)
			{
				jinwei=0;
				stac.push(‘0‘);
			}
			else if(*i-48+jinwei==1)
			{
				jinwei=0;
				stac.push(‘1‘);
			}
			else if(*i-48+jinwei==2)
			{
				jinwei=1;
				stac.push(‘0‘);
			}
		}
	}
	else if(i==a.rend()&&j!=b.rend())
	{
		for(;j!=b.rend();j++)
		{
			if(*j-48+jinwei==0)
			{
				jinwei=0;
				stac.push(‘0‘);
			}
			else if(*j-48+jinwei==1)
			{
				jinwei=0;
				stac.push(‘1‘);
			}
			else if(*j-48+jinwei==2)
			{
				jinwei=1;
				stac.push(‘0‘);
			}
		}
	}

	if(jinwei==1)
		stac.push(‘1‘);

	while(!stac.empty())
	{
		str.push_back(stac.top());
		stac.pop();
	}

	return str;



}

int main()
{
	string str1="1";
	string str2="111";
	
	cout<<addBinary(str1,str2)<<endl;

	system("pause");
	return 1;
}

  

 

leetcode_67题——Add Binary(字符串string,反向迭代器reverse_iterator,栈stack)

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4442478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!