题目描述:
给你两个二进制字符串,返回它们的和(用二进制表示)。输入为非空字符串且只包含数字 1
和 0
。
示例:
输入: a = "1010", b = "1011" 输出: "10101"
解题方法:
这道题与 LeetCode 题库中第 66 题不同的一点是每位数字相加,第 66 题只是最后一位数字相加,那么我们就不需要去额外处理最后一位数字了,直接全部在循环中处理就好。基本思路其实和第 66 题类似。先将传入的两个字符串转换为数组 charArray1[] 和 charArray2[],分别使用变量 i 和 j 去倒序遍历每一位数字,使用中间变量 resultArray[] 来记录每位数字相加的结果,使用 index 去进行 resultArray[] 的循环。因为需要考虑到进位,所以 resultArray[] 的长度比待加的两个数组中最长的多一个元素,赋值的逻辑是 charArray1[1] + charArray2[1] = resultArray[2],然后 resultArray[1] 用来记录是否产生进位。需要注意的是产生进位的有三种情况:charArray1[1] = '1' && charArray2[1] = '1' && resultArray[2] = 1;charArray1[1] = '1' && charArray2[1] = '1' && resultArray[2] = 0;charArray1[1] = '1' || charArray2[1] = '1' && resultArray[2] = 1。这三种情况对应产生的进位情况分别是:resultArray[2] = 1,resultArray[1] = 1;resultArray[2] = 0,resultArray[1] = 1;resultArray[2] = 0,resultArray[1] = 1。因为是二进制,所以我们可直接写死 resultArray[] 的每一位。就这样当处理完两个数组中的任何一个之后,再在循环外面去检查剩余的哪个数组还没有处理完,接着处理就行,逻辑类似,需要去判断下一位有没有进位的情况出现。全部处理完成之后得到了一个 int 类型的数组,将此数组转换为字符串并去掉第一位的 "0",得到的就是最终的结果。具体的数据处理过程可参考第 66 题的解法:66.加1_jiaomubai的博客-CSDN博客
本题代码如下:
public class AddBinary {
public static String addBinary(String a, String b) {
char[] charArray1 = a.toCharArray();
char[] charArray2 = b.toCharArray();
int[] resultArray = new int[Math.max(charArray1.length, charArray2.length) + 1];
int i = charArray1.length - 1;
int j = charArray2.length - 1;
int index = resultArray.length - 1;
for (; i >= 0 && j >= 0; i--, j--) {
if (resultArray[index] == 1) {
if (charArray1[i] == '1' && charArray2[j] == '1') {
// 进位产生 1 1 的情况
resultArray[index] = 1;
resultArray[index - 1] = 1;
} else if (charArray1[i] == '1' || charArray2[j] == '1') {
// 进位产生 1 0 的情况
resultArray[index] = 0;
resultArray[index - 1] = 1;
} else {
resultArray[index] = 1;
resultArray[index - 1] = 0;
}
} else {
if (charArray1[i] == '1' && charArray2[j] == '1') {
// 进位产生 1 0 的情况
resultArray[index] = 0;
resultArray[index - 1] = 1;
} else if (charArray1[i] == '1' || charArray2[j] == '1') {
resultArray[index] = 1;
resultArray[index - 1] = 0;
} else {
resultArray[index] = 0;
resultArray[index - 1] = 0;
}
}
index--;
}
// 如果 a 比 b 长
if (i >= 0) {
for (; i >= 0; i--) {
if (charArray1[i] == '1' && resultArray[index] == 1) {
resultArray[index] = 0;
resultArray[index - 1] = 1;
} else {
resultArray[index] = Integer.parseInt(String.valueOf(charArray1[i])) + Integer.parseInt(String.valueOf(resultArray[index]));
}
index--;
}
}
// 如果 b 比 a 长
if (j >= 0) {
for (; j >= 0; j--) {
if (charArray2[j] == '1' && resultArray[index] == 1) {
resultArray[index] = 0;
resultArray[index - 1] = 1;
} else {
resultArray[index] = Integer.parseInt(String.valueOf(charArray2[j])) + Integer.parseInt(String.valueOf(resultArray[index]));
}
index--;
}
}
String result = "";
for (int r = 1; r < resultArray.length; r++) {
result += resultArray[r];
}
if (resultArray[0] != 0) {
result = "1" + result;
}
return result;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
String a = "101111";
String b = "10";
String result = addBinary(a, b);
System.out.println("result = " + result);
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
}
}
除了这种方法之外,还有一种代码量特别少的方法,即直接使用 Java 的 BigInteger 类进行加法。
public static String addBinary2(String a, String b) {
BigInteger n1 = new BigInteger(a,2);
BigInteger n2 = new BigInteger(b,2);
BigInteger add = n1.add(n2);
return add.toString(2);
}