LeetCode --- 67. Add Binary
2015-07-30 14:24
246 查看
题目链接:Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
这道题的要求是两个二进制字符串加法运算。
简单的大数加法。仅仅只是是二进制的。处理进位的时候。依照二进制处理就可以。
时间复杂度:O(n)
空间复杂度:O(1)
转载请说明出处:LeetCode --- 67. Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11" b = "1" Return "100".
这道题的要求是两个二进制字符串加法运算。
简单的大数加法。仅仅只是是二进制的。处理进位的时候。依照二进制处理就可以。
时间复杂度:O(n)
空间复杂度:O(1)
1 class Solution 2 { 3 public: 4 string addBinary(string a, string b) 5 { 6 string s = ""; 7 8 int c = 0, i = a.size() - 1, j = b.size() - 1; 9 while(i >= 0 || j >= 0 || c == 1) 10 { 11 c += i >= 0 ? a[i --] - '0' : 0; 12 c += j >= 0 ? b[j --] - '0' : 0; 13 s = char(c % 2 + '0') + s; 14 c /= 2; 15 } 16 17 return s; 18 } 19 };
转载请说明出处:LeetCode --- 67. Add Binary
相关文章推荐
- MAC地址和IP地址的关系
- PAT (Advanced Level) 1083. List Grades (25) 结构体排序
- hdoj 2199 Can you solve this equation?
- 利用HttpWebRequest访问WebApi
- 在cisco交换机上禁止某个特定MAC地址转发
- 金典 SQL笔记(5)
- 160.Intersection of Two Linked Lists(两个单链表的相交结点)
- UML概念及其解释
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- jquery 杂记
- XMemcachedUtil
- PHP实现守护进程
- 网络中数据传输过程的分析
- 关于Myeclipse冲突的问题
- HDU 1283 最简单的计算机
- 关于web导航位置问题
- 配置算法(第4版)的Java编译环境
- ORA-00845: MEMORY_TARGET not supported on this system
- 文件上传之SWFUpload插件(代码)
- 数码管比段_宏定义