leetcode--Add Binary
来源:互联网 发布:js 变量给数组赋值 编辑:程序博客网 时间:2024/05/31 18:51
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
Discuss
class Solution {public: string addBinary(string a, string b) { int lena = a.size(); int lenb = b.size(); int i,j; if(lena==0||lenb==0)return a+b; string str; int carry = 0; int tem; for(i=lena-1, j=lenb-1;i>=0&&j>=0;i--,j--) { tem = (a[i]+b[j]+carry-2*'0')%2; carry = (a[i]+b[j]+carry-2*'0')/2; str = (char)(tem + '0') + str; } if(i>=0) { while(i!=-1) { tem = (a[i]+carry-'0')%2; carry = (a[i]+carry-'0')/2; str = (char)(tem + '0') + str; i--; } } if(j>=0) { while(j!=-1) { tem = (b[j]+carry-'0')%2; carry = (b[j]+carry-'0')/2; str = (char)(tem + '0') + str; j--; } } if(carry==1) str = '1'+str; return str; }};
0 0
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- LeetCode - Add Binary
- leetcode:Add Binary
- python default arg 和 *args 不能同时用
- 《黑马程序员》折半查找法实现及测试练习
- ARM 浮点运算,软浮点,硬浮点
- vs2010下vs2010 无效的数值参数“/Wno-deprecated
- Hive总结
- leetcode--Add Binary
- vlc-android 中调用用libvlcjni.so实现流媒体播放
- 编译原理(一)
- 记厂区停电,数据库及服务器关机异常
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 请教:过量喝咖啡对心脏的影响
- 消除“Unfortunately, System UI has stopped”的方法
- Thread.sleep(0)的意义
- oracle-em乱码问题解决