Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit76ac05c

Browse files
committed
Added Add Binary
1 parentc329092 commit76ac05c

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
classBigInt {
2+
private:
3+
string binary;
4+
5+
// Helper function to ensure strings are the same length
6+
staticvoidequalizeLength(string& a, string& b) {
7+
int lengthDifference = a.size() - b.size();
8+
if (lengthDifference >0)
9+
b.insert(0, lengthDifference,'0');
10+
else
11+
a.insert(0, -lengthDifference,'0');
12+
}
13+
14+
// Helper function to remove leading zeros
15+
voidremoveLeadingZeros() {
16+
// Erase leading zeros while keeping at least one zero if the number is
17+
// zero
18+
auto firstNonZero = binary.find_first_not_of('0');
19+
if (firstNonZero != string::npos) {
20+
binary.erase(0, firstNonZero);
21+
}else {
22+
binary =
23+
"0";// Adjust to maintain a minimum representation of zero
24+
}
25+
}
26+
27+
public:
28+
BigInt() : binary("0") {}
29+
30+
// Constructor from a binary string
31+
BigInt(const string& bin) : binary(bin) {removeLeadingZeros(); }
32+
33+
// Bitwise XOR
34+
BigIntoperator^(const BigInt& other)const {
35+
string a = binary;
36+
string b = other.binary;
37+
equalizeLength(a, b);
38+
string result;
39+
for (size_t i =0; i < a.size(); i++) {
40+
char xorChar = (a[i] == b[i] ?'0' :'1');
41+
result.push_back(xorChar);
42+
}
43+
returnBigInt(result);
44+
}
45+
46+
// Bitwise AND
47+
BigIntoperator&(const BigInt& other)const {
48+
string a = binary;
49+
string b = other.binary;
50+
equalizeLength(a, b);
51+
string result;
52+
for (size_t i =0; i < a.size(); i++) {
53+
char andChar = (a[i] =='1' && b[i] =='1' ?'1' :'0');
54+
result.push_back(andChar);
55+
}
56+
returnBigInt(result);
57+
}
58+
59+
// Left shift
60+
BigIntoperator<<(int shift)const {
61+
string result = binary;
62+
result.append(shift,'0');
63+
returnBigInt(result);
64+
}
65+
66+
// Check if BigInt is zero
67+
boolisZero()const {
68+
for (char c : binary)
69+
if (c !='0')returnfalse;
70+
returntrue;
71+
}
72+
73+
// Getter for binary string
74+
stringgetBinary()const {return binary; }
75+
};
76+
77+
classSolution {
78+
public:
79+
stringaddBinary(string a, string b) {
80+
BigIntx(a);
81+
BigInty(b);
82+
BigInt carry, answer;
83+
84+
while (!y.isZero()) {
85+
answer = x ^ y;
86+
carry = (x & y) <<1;
87+
x = answer;
88+
y = carry;
89+
}
90+
return x.getBinary();
91+
}
92+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importjava.math.BigInteger;
2+
3+
publicclassSolution {
4+
publicStringaddBinary(Stringa,Stringb) {
5+
BigIntegerx =newBigInteger(a,2);
6+
BigIntegery =newBigInteger(b,2);
7+
8+
while (y.compareTo(BigInteger.ZERO) !=0) {
9+
BigIntegeranswer =x.xor(y);
10+
BigIntegercarry =x.and(y).shiftLeft(1);
11+
x =answer;
12+
y =carry;
13+
}
14+
15+
returnx.toString(2);
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
varaddBinary=function(a,b){
2+
letx=BigInt("0b"+a);
3+
lety=BigInt("0b"+b);
4+
5+
while(y!==0n){
6+
letanswer=x^y;
7+
letcarry=(x&y)<<1n;
8+
x=answer;
9+
y=carry;
10+
}
11+
12+
returnx.toString(2);
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
classSolution:
2+
defaddBinary(self,a,b)->str:
3+
x,y=int(a,2),int(b,2)
4+
5+
whiley:
6+
answer=x^y
7+
carry= (x&y)<<1
8+
x,y=answer,carry
9+
10+
returnbin(x)[2:]
11+
# Time: O(A + B)
12+
# Space: O(max(A, B))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp