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

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)