LeetCode_Add Binary
来源:互联网 发布:阿里云服务器更换账号 编辑:程序博客网 时间:2024/06/15 14:07
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) { // Start typing your C/C++ solution below // DO NOT write int main() function if (a == "") return b; if (b == "") return a; int len1 = a.length(); int len2 = b.length(); int len3 = max(len1, len2); string ans(len3, '0'); int carry = 0; int i = len1 - 1; int j = len2 - 1; while ((i >= 0) || (j >= 0)) { char ca = '0'; if (i >= 0) { ca = a[i--]; } char cb = '0'; if (j >= 0) { cb = b[j--]; } int tmp = (ca - '0') + (cb - '0') + carry; carry = tmp / 2; tmp = tmp % 2; ans[--len3] = tmp + '0'; } if (carry == 1) { ans.insert(0, 1, '1'); } return ans; }};
- LeetCode_Add Binary
- Leetcode_add-binary
- Leetcode_Add Binary
- LeetCode_Add Binary
- Leetcode_Add Binary
- LeetCode_Add Binary
- leetcode_Add Binary
- LeetCode_Add Digits
- Leetcode_Add Two Numbers
- leetcode_Add Two Numbers
- LeetCode_Add Two Numbers
- Leetcode_Add Two Numbers
- LeetCode_Add Two Numbers
- LeetCode_Add Two Numbers
- leetcode_add two nums
- LeetCode_Add Two Numbers(Medium)
- leetcode_add two number
- leetCode_Add and Search Word - Data structure design
- Solrj操作Solr4.0/Solr4.3中CloudSolrServer的一般过程
- 顺序查找
- 如何用VS2010查看汇编代码
- RK29-31全系列均适用的开机LOGO第一屏替换教 ...
- 二分查找
- LeetCode_Add Binary
- 按某元素对二维数组排序
- gcc与g++的编译链接的示例详解
- Oracle with子句
- 向二维数组中添加元素
- xshell自动登录脚本
- 三个星期从0开始 制作IOS游戏并提交上架
- MySQL的表锁问题(二)——InnoDB表锁问题
- 打印图形