Skip to content

Commit 8304f20

Browse files
committed
Changed variable names for clarity
1 parent 76ac05c commit 8304f20

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

Add Binary - Leetcode 67/Add Binary - Leetcode 67.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
public class Solution {
44
public String addBinary(String a, String b) {
5+
// Convert the binary strings to BigInteger
56
BigInteger x = new BigInteger(a, 2);
67
BigInteger y = new BigInteger(b, 2);
78

9+
// Perform the binary addition using bitwise operations
810
while (y.compareTo(BigInteger.ZERO) != 0) {
9-
BigInteger answer = x.xor(y);
11+
BigInteger withoutCarry = x.xor(y);
1012
BigInteger carry = x.and(y).shiftLeft(1);
11-
x = answer;
13+
x = withoutCarry;
1214
y = carry;
1315
}
1416

17+
// Convert the result back to binary and return it as a string
1518
return x.toString(2);
1619
}
1720
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
var addBinary = function(a, b) {
2+
// Convert the binary strings to BigInt
23
let x = BigInt("0b" + a);
34
let y = BigInt("0b" + b);
45

6+
// Perform the binary addition using bitwise operations
57
while (y !== 0n) {
6-
let answer = x ^ y;
8+
let withoutCarry = x ^ y;
79
let carry = (x & y) << 1n;
8-
x = answer;
10+
x = withoutCarry;
911
y = carry;
1012
}
1113

14+
// Convert the result back to binary and return it as a string
1215
return x.toString(2);
1316
};
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def addBinary(self, a, b) -> str:
3-
x, y = int(a, 2), int(b, 2)
3+
a, b = int(a, 2), int(b, 2)
44

5-
while y:
6-
answer = x ^ y
7-
carry = (x & y) << 1
8-
x, y = answer, carry
5+
while b:
6+
without_carry = a ^ b
7+
carry = (a & b) << 1
8+
a, b = without_carry, carry
99

10-
return bin(x)[2:]
10+
return bin(a)[2:]
1111
# Time: O(A + B)
1212
# Space: O(max(A, B))

0 commit comments

Comments
 (0)