classSolution { public: string addBinary(string a, string b){ string result = ""; int carry = 0; int i = a.length() - 1, j = b.length() - 1; while (i >= 0 || j >= 0) { int sum = carry; if (i >= 0) { sum += a[i] - '0'; i--; } if (j >= 0) { sum += b[j] - '0'; j--; } result = to_string(sum % 2) + result; carry = sum / 2; } if (carry) { result = "1" + result; } return result; } };
classSolution(object): defaddBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ result = "" carry = 0 i, j = len(a) - 1, len(b) - 1 while i >= 0or j >= 0: sum = carry if i >= 0: sum += int(a[i]) i -= 1 if j >= 0: sum += int(b[j]) j -= 1 result = str(sum % 2) + result carry = sum // 2 if carry: result = "1" + result return result
结果
执行用时 : 26 ms, 击败 23.78% 使用 Python 的用户
内存消耗 : 11.61 MB, 击败 25.55% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defaddBinary(self, a: str, b: str) -> str: result = "" carry = 0 i, j = len(a) - 1, len(b) - 1 while i >= 0or j >= 0: sum = carry if i >= 0: sum += int(a[i]) i -= 1 if j >= 0: sum += int(b[j]) j -= 1 result = str(sum % 2) + result carry = sum // 2 if carry: result = "1" + result return result
char* addBinary(char* a, char* b) { int lenA = strlen(a); int lenB = strlen(b); int lenResult = lenA > lenB ? lenA + 2 : lenB + 2; char* result = (char*)malloc(lenResult * sizeof(char)); int carry = 0; int i = lenA - 1, j = lenB - 1; int k = 0; while (i >= 0 || j >= 0 || carry) { int sum = carry; if (i >= 0) { sum += a[i] - '0'; i--; } if (j >= 0) { sum += b[j] - '0'; j--; } result[k++] = sum % 2 + '0'; carry = sum / 2; } for (int l = 0; l < k / 2; l++) { char temp = result[l]; result[l] = result[k - l - 1]; result[k - l - 1] = temp; } result[k] = '\0'; return result; }
publicclassSolution { publicstringAddBinary(string a, string b) { StringBuilder result = new StringBuilder(); int carry = 0; int i = a.Length - 1, j = b.Length - 1; while (i >= 0 || j >= 0 || carry != 0) { int sum = carry; if (i >= 0) { sum += a[i] - '0'; i--; } if (j >= 0) { sum += b[j] - '0'; j--; } result.Insert(0, sum % 2); carry = sum / 2; } return result.ToString(); } }
/** * @param {string} a * @param {string} b * @return {string} */ var addBinary = function(a, b) { let result = ""; let carry = 0; let i = a.length - 1; let j = b.length - 1; while (i >= 0 || j >= 0 || carry !== 0) { let sum = carry; if (i >= 0) { sum += parseInt(a[i]); i--; } if (j >= 0) { sum += parseInt(b[j]); j--; } result = (sum % 2) + result; carry = Math.floor(sum / 2); } return result; };
functionaddBinary(a: string, b: string): string { let result = ""; let carry = 0; let i = a.length - 1; let j = b.length - 1; while (i >= 0 || j >= 0 || carry !== 0) { let sum = carry; if (i >= 0) { sum += parseInt(a[i]); i--; } if (j >= 0) { sum += parseInt(b[j]); j--; } result = (sum % 2) + result; carry = Math.floor(sum / 2); } return result; }
classSolution { funcaddBinary(_a: String, _b: String) -> String { var result ="" var carry =0 var i = a.count -1 var j = b.count -1 let aChars =Array(a) let bChars =Array(b) while i >=0|| j >=0|| carry !=0 { var sum = carry if i >=0 { sum +=Int(String(aChars[i])) ??0 i -=1 } if j >=0 { sum +=Int(String(bChars[j])) ??0 j -=1 } result ="\(sum %2)"+ result carry = sum /2 } return result } }
classSolution { funaddBinary(a: String, b: String): String { var result = "" var carry = 0 var i = a.length - 1 var j = b.length - 1 while (i >= 0 || j >= 0 || carry != 0) { var sum = carry if (i >= 0) { sum += a[i] - '0' i-- } if (j >= 0) { sum += b[j] - '0' j-- } result = (sum % 2).toString() + result carry = sum / 2 } return result } }
classSolution{ String addBinary(String a, String b) { StringBuffer result = StringBuffer(); int carry = 0; int i = a.length - 1; int j = b.length - 1; while (i >= 0 || j >= 0 || carry != 0) { int sum = carry; if (i >= 0) { sum += int.parse(a[i]); i--; } if (j >= 0) { sum += int.parse(b[j]); j--; } result.write(sum % 2); carry = sum ~/ 2; } return result.toString().split('').reversed.join(); } }
结果
执行用时 : 328 ms, 击败 66.67% 使用 Dart 的用户
内存消耗 : 147.41 MB, 击败 66.67% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcaddBinary(a string, b string)string { result := "" carry := 0 i, j := len(a) - 1, len(b) - 1 for i >= 0 || j >= 0 || carry != 0 { sum := carry if i >= 0 { sum += int(a[i] - '0') i-- } if j >= 0 { sum += int(b[j] - '0') j-- } result = strconv.Itoa(sum % 2) + result carry = sum / 2 } return result }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户
内存消耗 : 3.58 MB, 击败 20.85% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# @param {String} a # @param {String} b # @return {String} defadd_binary(a, b) result = "" carry = 0 i = a.length - 1 j = b.length - 1 while i >= 0 || j >= 0 || carry != 0 sum = carry sum += a[i].to_i if i >= 0 sum += b[j].to_i if j >= 0 result = (sum % 2).to_s + result carry = sum / 2 i -= 1 j -= 1 end result end
objectSolution{ defaddBinary(a: String, b: String): String = { val result = newStringBuilder() var carry = 0 var i = a.length - 1 var j = b.length - 1 while (i >= 0 || j >= 0 || carry != 0) { var sum = carry if (i >= 0) { sum += a(i) - '0' i -= 1 } if (j >= 0) { sum += b(j) - '0' j -= 1 } result.insert(0, sum % 2) carry = sum / 2 } result.toString() } }