#include<iostream>#include<string>usingnamespacestd;classSolution{public:stringAddBinary(string a, string b){size_tsize = a.size() > b.size() ? a.size() : b.size();reverse(a.begin(), a.end());reverse(b.begin(), b.end());intCarryBit =0;// 进位string result;// 用于存放结果...
Given two binary strings, return their sum (also a binary string). For example, a ="11" b ="1" Return"100". 解题思路1:判断当前字符所表示的数字,产生输出和进位。缺点:程序比较复杂。 代码1: class Solution { public: string addBinary(string a, string b) { char carry='0'; string c; ...
代码 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 1.题目 Given two binary strings, return their sum (also a binary string). For example, a = "11"b = "1"Return "100". 2.解答 classSolution{ public: charintToChar(intinput){ charstrBuffer[1]={0}; sprintf(s...
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;...
Leetcode 之Add Binary(29) 比较简单,细节:先将字符串翻转,注意进位。 stringaddBinary(stringa,stringb) {stringresult;intlen = a.size() > b.size() ?a.size() : b.size(); reverse(a.begin(), a.end()); reverse(b.begin(), b.end());intcarry =0;for(inti =0; i < len; i++)...
classSolution:defaddBinary(self,a:str,b:str)->str:sumInt=int(a,2)+int(b,2)sumBin=bin(sumInt)#string starts with '0b'returnsumBin[2:]# equally, but more precise# return bin( int(a, 2) + int(b, ) )[2:]# return '{:b}'.format(int(a, 2) + int(b, 2))# return f"...
Add Binary https://leetcode.com/problems/add-binary/description/ 主要是全加器的公式: s = abc c = (a^b)&c | (a&b) 其他的没什么 就去一下零处理 以及颠倒一下列表 classSolution(object):defaddBinary(self,a,b):""" :type a: str...
Add Binary 二进制数求和 128 -- 6:56 App [LeetCode] 27. Remove Element 移除元素 102 -- 15:04 App [LeetCode] 71. Simplify Path 简化路径 23 -- 9:03 App [LeetCode] 16. 3Sum Closest 最接近的三数之和 20 -- 13:44 App [LeetCode] 15. 3Sum 三数之和 26 -- 15:53 App ...
67. 二进制求和 - 给你两个二进制字符串 a 和 b ,以二进制字符串的形式返回它们的和。 示例 1: 输入:a = "11", b = "1" 输出:"100" 示例 2: 输入:a = "1010", b = "1011" 输出:"10101" 提示: * 1 <= a.length, b.length <= 104 * a 和 b 仅由字符 '0