67. Add Binary
来源:互联网 发布:php批量解压文件修改 编辑:程序博客网 时间:2024/05/22 20:38
1.需求
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
2.代码
#include <iostream>#include <string>using namespace std;class Solution {public: string addBinary(string a, string b) { int c = 0; int i = a.length() - 1; int j = b.length() - 1; string result = ""; while(i >= 0 || j >= 0 || c == 1){ c += i >= 0 ? a[i--]-'0':0; c += j >= 0 ? b[j--]-'0':0; result= (char)(c % 2 + '0') + result; c /= 2; } return result; }};int main(){ Solution s; cout << s.addBinary("11","1110") << endl;}
参考资料:
http://blog.csdn.net/wangyaninglm/article/details/46766027
0 0
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 67. Add Binary
- 【leetcode】67. Add Binary
- 67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 67. Add Binary
- 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary
- 67. Add Binary LeetCode
- 67. Add Binary
- 【LeetCode】67. Add Binary
- 67. Add Binary
- 67. Add Binary
- 大学就是修心
- 为什么会有article和aside不同的标签
- 在myeclipse中 移动多行代码
- python学习手册 简记
- git之从远程库克隆
- 67. Add Binary
- android学习笔记——Bitmaps的使用(处理大图时防止OOM)
- java对redis的基本操作
- js实现复制到剪切板,兼容所有浏览器
- C/C++结构体字节对齐详解
- NavigationBar底部有条横线怎么办
- 如何扩展jquery插件
- MYSQL_insert,update,select一体化
- 画圆不整