Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
class Solution { public: string addBinary(string a, string b) { if(a.size() == 0) return b; if(b.size() == 0) return a; string ret; int carry = 0; int i = a.size() - 1; int j = b.size() - 1; while(i>=0 && j>=0) { int tmp = a[i] - '0' + b[j] - '0' + carry; carry = tmp / 2; ret = (char)('0' + tmp%2) + ret; --i; --j; } while(i>=0) { int tmp = a[i] - '0' + carry; carry = tmp/2; ret = (char)('0' + tmp%2) + ret; --i; } while(j>=0) { int tmp = b[j] - '0' + carry; carry = tmp/2; ret = (char)('0' + tmp%2) + ret; --j; } if(carry) ret = (char)('0' + carry) + ret; return ret; } };
欢迎关注微信公众号——计算机视觉:
相关推荐
leetCode 第67题。Swift解
python对图像提取mask部分: 代码: ...# Add binary masks to images for img_item in os.listdir(images_path): print(img_item) img_path = os.path.join(images_path, img_item) img = cv2.
Add Binary Longest Palindromic Substring Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman Roman to Integer Count and Say Anagrams Valid Anagram ...
算法类,二进制加法的源代码,算法导论上的
401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](./C++/binary-watch.cpp) [Python](./Python/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | 411 | [Minimum Unique Word ...
pod repo add binary_spec git@github.com:caixiang305621856/open_binary_spec.git 2.安装mongodb # 进入 /usr/local cd /usr/local # 下载 sudo curl -O ...
两个n位二进制数分别存储在两个n元数组A和B中,这两个整数的和存在一个n+1元的数组C中 答: 此问题主要是考察相加进位的问题,元素1+1 =0 并且往前进一位 ADD-BINARY(A,B) ...function addBinary($A
题目实现一个二进制加法,输入输出均为二进制字符串function binaryAdd(num1: string, num2: string): string {
leetcode ...Binary Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法
67.二进制求和 (Add Binary) 69.x 的平方根 (Sqrt(x)) 70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 ...
04-23从Alphabet到IntegerMapping的DecryptString 2020-04-25合并两个已排序的NodeList Fibonacci 2020-04-26查找ContinuousSequence MaximunNum 2020-04-28递归多态2020-04-30 HappyNumber 2020-05-01 AddBinary ...
AddBinary:从字符串中添加两个二进制数 AddTwoNumber:从数组中添加两个数字 BSTtoDLL:将二进制搜索树转换为双链表 BTreeIterator:二叉树迭代器:有序,前序,后序 BuildBT:从数组输入构建二进制 爬楼梯:爬...
addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 ...
leetcode双人赛LeetCode ...AddBinary :简单的问题。 AddTwoNumbers :简单的问题。 Anagrams :简单的#hashtable问题。 BalancedBinaryTree :简单的#balance #tree问题。 BestTimetoBuyandSellStock :简单的问题。 ...
所有题目源代码:Git地址 题目 给你两个二进制字符串,返回它们的和(用二进制表示)。 输入为 非空 字符串且只包含数字 1 和 0。 示例 1: 输入: a = 11, b = 1 ... public String addBinary(String a,
Introduction ============ This is a class to calculate histogram of LBP (local binary patterns) from an input image, histograms of...* Add the calculation of histogram of uniform rotation invariant VLBP.
此程序采用VHDL语言,完成在16位十六进制加法器的基础上将输出进行BCD码转换,实现输出是BCD码的16位二进制加法器
BinarySerializer ... AddFile ( new LinearSerializedFile ( context ) { FilePath = relativeFilePath }); 使用不同的BinaryFile类型,例如LinearSerializedFile , MemoryMappedFile和StreamF
此程序采用VHDL语言,完成在32位十六进制加法器的基础上将输出进行BCD码转换,实现输出是BCD码的32位二进制加法器
Work with Unity 5 and Unity 4.x ...– Add 3D Binary – Add 3D Computer – Add 3D Snow – Add 3D Black Hole – Add 3D Ghost Light – Add 3D Anomaly – Add 3D Shield – Add Atmosphere Fog – Add Pixelisat