2.2 大神算法 参考:https://leetcode.com/problems/add-binary/discuss/24475/Short-code-by-c%2B%2B classSolution{public:stringaddBinary(stringa,stringb){stringresultStr ="";intcarry =0, aIndex = a.size() -1, bIndex = b.size() -1;//初始化余数和a,b索引值while(aIndex >=0|| bIndex...
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 class Solution { 5 public: 6 string addBinary(string a, string b) { 7 // Start typing your C/C++ solution below 8 // DO NOT write int main() function 9 int tlen, alen, blen; 10 string c; 11 alen = a....
public: string addBinary(string a, string b) { int temp,len_a,len_b; len_a = a.size(); len_b = b.size(); string::iterator itr_a = a.end() - 1; string::iterator itr_b = b.end() - 1; string str_a =a; string str_b =b; if(len_a<len_b) { a =str_b; b =st...
https://leetcode.com/problems/add-binary/ 知道二进制加法原理即可,这里只需要知道进位是除数,余数是结果就行。最后不要忽略reg里面的值 class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ if not a: return b elif not b: return a tmp_...
usestd::iter; implSolution{ pubfnadd_binary(a:String,b:String)->String{ //进位,初始为0 letmutcarry=0; //收集a和b按位加的结果 letmutresult=a //返回底层的字节切片 .as_bytes() //转换成迭代器 .iter() //提前反向迭代(后面会加上无限的'0',所以不能后面同时反向) .rev() //在a后面...
Leetcode - Add Binary Paste_Image.png My code: public class Solution { public String addBinary(String a, String b) { if (a == null || a.length() == 0 || b == null || b.length() == 0) return null; int len = Math.max(a.length(), b.length()) + 1;...
https://leetcode.com/problems/add-binary/description/ 主要是全加器的公式: s = abc c = (a^b)&c | (a&b) 其他的没什么 就去一下零处理 以及颠倒一下列表 classSolution(object):defaddBinary(self,a,b):""" :type a: str :type b: str ...
LeetCode 67 Add Binary(二进制相加)(*) 翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串)。 例如,a="11"b ="1"返回"100". 原文 Giventwobinary strings,returntheirsum(alsoabinarystring). For example,a="11"b ="1"Return"100"....
LeetCode 0067 - Add Binary的解题思路是什么? 如何用Python实现LeetCode 0067 - Add Binary? LeetCode 0067 - Add Binary的时间复杂度是多少? Add Binary Desicription Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Solution 代...
Given two binary stringsaandb, returntheir sum as a binary string. Example 1: Input:a = "11", b = "1"Output:"100" Example 2: Input:a = "1010", b = "1011"Output:"10101" Constraints: 1 <= a.length, b.length <= 104