Easy
,Math
,String
,Bit Manipulation
Given two binary strings a
and b
, return their sum as a binary string.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
Constraints:
a.length
, b.length
<= 104a
and b
consist only of '0'
or '1'
characters.
class Solution:
def addBinary(self, a: str, b: str) -> str:
x, y = int(a, 2), int(b, 2)
while y:
x, y = x ^ y, (x & y) << 1
return bin(x)[2:]
Ron ChenFeb 14, 2023
function addBinary(a, b) {
return (BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2);
}
學到js新用法,但這樣寫好像不是題目想要的XDD
MarsgoatFeb 14, 2023