67. Add Binary
2016-01-06 15:32
183 查看
1.需求
Given two binary strings, return their sum (also a binary string).
For example,
a = “11”
b = “1”
Return “100”.
2.代码
参考资料:
/article/1323939.html
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; }
参考资料:
/article/1323939.html
相关文章推荐
- JS-表格数据的添加与删除、搜索
- Android——getSystemService
- [Javascript] Array methods in depth - some
- overflow与flex同时使用发现的问题
- 如何用 React Native 创建一个iOS APP?
- poj 3268 Silver Cow Party
- VirtualBox 删掉虚拟镜像重新安装失败
- 第十二课 定位画图---坐标
- iOS 消息推送
- 看懂执行计划 顺序
- Java并发编程:并发基础概念
- js对象或类的方法设计模式解读
- leetcode49 Group Anagrams
- MyEclipse 快捷键大全(@Hcy)
- 远程连接服务器的方法:
- 获取MAC地址
- iOS开发-完整学习路线图
- Matlab从三维矩阵中取出一列并且reshape
- swift获取图片像素颜色值
- apistore接口调用demo