-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBinary.java
79 lines (70 loc) · 1.72 KB
/
AddBinary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* 题目:给定两个字符串,返回他们的加和(二进制表示),加和也是字符串
* 解题思路:
* 1、首先,两个字符串的大小可能不同,我们则需要在较短的字符串前面加0,以使得两个字符串的长度相等。
* 2、然后从两个字符串的末尾开始取字符,相加。
* 结果有三种:=0:本位=0、进位=0;
* =1:本位=1、进位=0;
* =2:本位=0、进位=1;
* =3:本位=1、进位=1
*
*/
import java.util.*;
public class AddBinary {
public static void main(String[] args) {
System.out.println("请输入字符串a:");
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
System.out.println("请输入字符串b:");
String b=sc.nextLine();
Solution155 sl=new Solution155();
System.out.println("结果是:"+sl.addBinary(a, b));
}
}
class Solution155
{
public String addBinary(String a,String b)
{
String result="";
if(a==null) return b;
if(b==null) return a;
//将短的字符串前面补0
int Lena=a.length();
int Lenb=b.length();
if(Lena>Lenb){for(int i=0;i<Lena-Lenb;i++) b="0"+b;}
if(Lena<Lenb){for(int i=0;i<Lenb-Lena;i++) a="0"+a;}
//定义进位carry
int Len=Math.max(Lena,Lenb);
boolean carry=false;
int cur=0;
for(int i=Len-1;i>=0;i--)
{
if(carry)
cur=(a.charAt(i)-'0')+(b.charAt(i)-'0')+1;
else
cur=(a.charAt(i)-'0')+(b.charAt(i)-'0');
if(cur==0)
{
result="0"+result;
carry=false;
}
else if(cur==1)
{
result="1"+result;
carry=false;
}
else if(cur==2)
{
result="0"+result;
carry=true;
}
else if(cur==3)
{
result="1"+result;
carry=true;
}
}
if(carry) result="1"+result;
return result;
}
}