二进制求和

题目描述:给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-binary/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:分组处理

由于过长的字符串直接转成int值会报错,因为超出了int值的上线,所以采取分段处理的方式,从后往前,每30位相加,利用了Integer.parseInt(String, 2)方法将字符串二进制数转成10进制的int值,然后利用Integer.toBinaryString(int)方法将相加后的值再转成二进制字符串,然后把分段的字符串加起来,就是最后的结果。

做的过程中有几点要考虑:

  • 第一,每段相加时,考虑是否有进位,如果有进位,用addOne表示进位数,值为1,在下一分段相加时把addOne加上;
  • 第二,当每段相加的结果长度小于30位时,需要在前面补0,补到30位。
  1. public class LeetCode_067 {
  2. public static String addBinary(String a, String b) {
  3. int addOne = 0;
  4. String result = "";
  5. while (a.length() > 30 || b.length() > 30) {
  6. String aStr;
  7. String bStr;
  8. if (a.length() > 30) {
  9. aStr = a.substring(a.length() - 30, a.length());
  10. a = a.substring(0, a.length() - 30);
  11. } else {
  12. aStr = a;
  13. a = "0";
  14. }
  15. if (b.length() > 30) {
  16. bStr = b.substring(b.length() - 30, b.length());
  17. b = b.substring(0, b.length() - 30);
  18. } else {
  19. bStr = b;
  20. b = "0";
  21. }
  22. String temp = Integer.toBinaryString(Integer.parseInt(aStr, 2) + Integer.parseInt(bStr, 2) + addOne);
  23. if (temp.length() > 30) {
  24. addOne = 1;
  25. temp = temp.substring(1, temp.length());
  26. } else {
  27. if(temp.length() < 30) {
  28. int zeroCount = 30 - temp.length();
  29. for(int i = 0; i < zeroCount; i++) {
  30. temp = "0" + temp;
  31. }
  32. }
  33. addOne = 0;
  34. }
  35. result = temp + result;
  36. }
  37. return Integer.toBinaryString(Integer.parseInt(a, 2) + Integer.parseInt(b, 2) + addOne) + result;
  38. }
  39. public static void main(String[] args) {
  40. String a = "1001101011011010000010111010100111001000100001111110011111001010100101111";
  41. String b = "111000011000010000001100001001010011000101000000001111101101000100000000100100001100010000111001000";
  42. System.out.println(addBinary(a, b));
  43. }
  44. }

【每日寄语】 天可补,海可填,南山可移。日月既往,不可复追。