Moment For Technology

Leetcode-067 - Binary Sum

Posted on Aug. 10, 2023, 2:34 p.m. by Patricia Randolph
Category: java Tag: java algorithm leetcode LeetCode Personal Problem Solving Summary binary

Binary summation

Description: Give you two binary strings and return their sum (in binary).

The input is a non-empty string and contains only the digits 1 and 0.

See the LeetCode website for an example.

Source: LeetCode Link: https://leetcode-cn.com/probl... Copyright belongs to collar network. Commercial reprint please contact the official authorization, non-commercial reprint please indicate the source.

Solution 1: group processing

ParseInt (String, 2) is used to convert the binary number of a String into a decimal int. This method is used to convert the binary number of a String into a decimal int. This method is used to convert the binary number of a String into a decimal int. The integer.tobinaryString (int) method is then used to convert the added value back to a binary string, and the pieced strings are added up to produce the final result.

There are several points to consider in the process of doing this:

  • First, when adding each segment, consider whether there is a carry. If there is a carry, use addOne to represent the carry number with a value of 1, and add addOne to the next segment.
  • Second, when the length of each segment is less than 30 digits, it is necessary to add 0 in front of it to 30 digits.
public class LeetCode_067 { public static String addBinary(String a, String b) { int addOne = 0; String result = ""; while (a.length()  30 || b.length()  30) { String aStr; String bStr; if (a.length()  30) { aStr = a.substring(a.length() - 30, a.length()); a = a.substring(0, a.length() - 30); } else { aStr = a; a = "0"; } if (b.length()  30) { bStr = b.substring(b.length() - 30, b.length()); b = b.substring(0, b.length() - 30); } else { bStr = b; b = "0"; } String temp = Integer.toBinaryString(Integer.parseInt(aStr, 2) + Integer.parseInt(bStr, 2) + addOne); if (temp.length()  30) { addOne = 1; temp = temp.substring(1, temp.length()); } else { if(temp.length()  30) { int zeroCount = 30 - temp.length(); for(int i = 0; i  zeroCount; i++) { temp = "0" + temp; } } addOne = 0; } result = temp + result; } return Integer.toBinaryString(Integer.parseInt(a, 2) + Integer.parseInt(b, 2) + addOne) + result; } public static void main(String[] args) { String a = "1001101011011010000010111010100111001000100001111110011111001010100101111"; String b = "111000011000010000001100001001010011000101000000001111101101000100000000100100001100010000111001000"; System.out.println(addBinary(a, b)); }}
【 Daily Message 】 Heaven can be filled, the sea can be filled, the south mountain can be moved. The sun and the moon cannot be traced back.
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.